Mapping markers

This page describes how to create a marker & mapping them.
If you want to a point on the map, you may put a marker.

Using a marker, you can put on the map anywhere and describe a point of location.



code

<script type='text/javascript'>

    var mapCanvas;
    function intialize() {
      //Create a map
      mapCanvas = new google.maps.Map(document.getElementById("map_canvas"));
      mapCanvas.setCenter(new google.maps.LatLng(38.196424,-147.130884));
      mapCanvas.setZoom(2);
      mapCanvas.setMapTypeId(google.maps.MapTypeId.ROADMAP);
      
      
      //marker1 : New York, USA
      var marker1 = new google.maps.Marker();
      marker1.setPosition(new google.maps.LatLng(40.714353, -74.005973));
      marker1.setMap(mapCanvas);
      
      //marker2 : Tokyo, Japan
      var marker2 = new google.maps.Marker({
        position : new google.maps.LatLng(35.678494,139.744205),
        map: mapCanvas
      });
    }
    google.maps.event.addDomListener(window, "load", intialize);
  </script>

Description

Here is key point:
var marker1 = new google.maps.Marker();
marker1.setPosition(new google.maps.LatLng(35.676148,139.74479));
marker1.setMap(mapCanvas);
var marker = new google.maps.Marker();

Create a marker instance. If you specify MarkerOptions structure to arguments, marker is initialized at one time like marker2.

google.maps.Marker(MarkerOptions)

MarkerOptions : (Optional) Specifys marker options


marker.setPosition(...); Specifys a latlng pair to point on the map.

marker.setMap(mapCanvas); Specifys a map.

Adds just only three lines to code, you may show a marker on the map.


Search
Books
Google Maps API Programming Guide

Four Google Maps API Experts wrote this book. Introducing useful examples for developers!
See more details