- Introduction
- Add a marker
- Remove a marker
- Animate a marker
- Customize a marker image
- Make a marker draggable
Introduction
A marker identifies a location on a map. By default, a marker uses a standard
image. Markers can display custom images, in which case they are usually
referred to as "icons." Markers and icons are objects of type
Marker. You can set a custom icon within the marker's
constructor, or by calling setIcon() on the marker. See more
about customizing the marker image below.
Broadly speaking, markers are a type of overlay. For information on other types of overlay, see Drawing on the map.
Markers are designed to be interactive. For example, by default they receive
'click' events, so you can add an event listener to bring up an
info window
displaying custom information. You can allow users to move a marker on the map
by setting the marker's draggable property to true.
For more information about draggable markers, see
below.
Add a marker
The google.maps.Marker constructor takes a single
Marker options object literal, specifying the initial
properties of the marker.
The following fields are particularly important and commonly set when constructing a marker:
position(required) specifies aLatLngidentifying the initial location of the marker.map(optional) specifies theMapon which to place the marker. If you do not specify the map on construction of the marker, the marker is created but is not attached to (or displayed on) the map. You may add the marker later by calling the marker'ssetMap()method.
The following example adds a simple marker to a map at Uluru, in the center of Australia:
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// To add the marker to the map, use the 'map' property
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
In the above example, the marker is placed on the map at construction of the
marker using the map property in the marker options.
Alternatively, you can add the marker to the map directly by using the
marker's setMap() method, as shown in the example below:
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
title:"Hello World!"
});
// To add the marker to the map, call setMap();
marker.setMap(map);
The marker's title will appear as a tooltip.
If you do not wish to pass any Marker options in the
marker's constructor, instead pass an empty object {} in the
last argument of the constructor.
View example (marker-simple.html).
Remove a marker
To remove a marker from the map, call the setMap() method
passing null as the argument.
marker.setMap(null);
Note that the above method does not delete the marker. It simply removes
the marker from the map. If instead you wish to delete the marker,
you should remove it from the map, and then set the
marker itself to null.
If you wish to manage a set of markers, you should create
an array to hold the markers. Using this array, you can then call
setMap() on each marker in the array in turn when you need to
remove the markers. You can delete
the markers by removing them from the map and then setting the
array's length to 0, which removes all
references to the markers.
View example (marker-remove.html).
Animate a marker
You can animate markers so that they exhibit dynamic movement in a
variety of different circumstances. To specify the way a marker is animated,
use the marker's animation property, of type
google.maps.Animation. The following Animation
values are supported:
DROPindicates that the marker should drop from the top of the map to its final location when first placed on the map. Animation will cease once the marker comes to rest andanimationwill revert tonull. This type of animation is usually specified during creation of theMarker.BOUNCEindicates that the marker should bounce in place. A bouncing marker will continue bouncing until itsanimationproperty is explicitly set tonull.
You may initiate an animation on an existing marker by calling
setAnimation() on the Marker object.
// The following example creates a marker in Stockholm, Sweden
// using a DROP animation. Clicking on the marker will toggle
// the animation between a BOUNCE animation and no animation.
var stockholm = new google.maps.LatLng(59.32522, 18.07002);
var parliament = new google.maps.LatLng(59.327383, 18.06747);
var marker;
var map;
function initialize() {
var mapOptions = {
zoom: 13,
center: stockholm
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
marker = new google.maps.Marker({
map:map,
draggable:true,
animation: google.maps.Animation.DROP,
position: parliament
});
google.maps.event.addListener(marker, 'click', toggleBounce);
}
function toggleBounce() {
if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
View example (marker-animations.html).
If you have many markers, you might not want to drop them on the map
all at once. You can make use of setTimeout() to space your
markers' animations using a pattern like that shown below:
function drop() {
for (var i =0; i < markerArray.length; i++) {
setTimeout(function() {
addMarkerMethod();
}, i * 200);
}
}
View example (marker-animations-iteration.html).
Customize a marker image
Markers may define an icon to show instead of the default icon. Defining an icon involves setting a number of properties that define the visual behavior of the marker. The sections below describe simple icons, complex icons, and symbols (vector icons).
Simple icons
In the most basic case, an icon can simply indicate an image to
use instead of the default Google Maps pushpin icon. To specify such an icon,
set the marker's icon property to the URL of an image.
The Google Maps API will size the icon automatically.
// This example adds a marker to indicate the position
// of Bondi Beach in Sydney, Australia
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(-33, 151)
}
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var image = 'images/beachflag.png';
var myLatLng = new google.maps.LatLng(-33.890542, 151.274856);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image
});
}
google.maps.event.addDomListener(window, 'load', initialize);
View example (icon-simple.html).
Complex icons
You may want to specify complex shapes to indicate regions that are
clickable, and specify how the icons should appear relative to
other overlays (their "stack order"). Icons specified in this manner should
set their icon properties to an object of type
Icon.
Icon objects define an image. They also
define the size of the icon, the origin
of the icon (if the image you want is part of a larger image in a sprite,
for example), and the anchor where the icon's hotspot
should be located (which is based on the origin).
// The following example creates complex markers to indicate beaches near
// Sydney, NSW, Australia. Note that the anchor is set to
// (0,32) to correspond to the base of the flagpole.
function initialize() {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(-33.9, 151.2)
}
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
setMarkers(map, beaches);
}
/**
* Data for the markers consisting of a name, a LatLng and a zIndex for
* the order in which these markers should display on top of each
* other.
*/
var beaches = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
function setMarkers(map, locations) {
// Add markers to the map
// Marker sizes are expressed as a Size of X,Y
// where the origin of the image (0,0) is located
// in the top left of the image.
// Origins, anchor positions and coordinates of the marker
// increase in the X direction to the right and in
// the Y direction down.
var image = {
url: 'images/beachflag.png',
// This marker is 20 pixels wide by 32 pixels tall.
size: new google.maps.Size(20, 32),
// The origin for this image is 0,0.
origin: new google.maps.Point(0,0),
// The anchor for this image is the base of the flagpole at 0,32.
anchor: new google.maps.Point(0, 32)
};
// Shapes define the clickable region of the icon.
// The type defines an HTML <area> element 'poly' which
// traces out a polygon as a series of X,Y points. The final
// coordinate closes the poly by connecting to the first
// coordinate.
var shape = {
coords: [1, 1, 1, 20, 18, 20, 18 , 1],
type: 'poly'
};
for (var i = 0; i < locations.length; i++) {
var beach = locations[i];
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image,
shape: shape,
title: beach[0],
zIndex: beach[3]
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
View example (icon-complex.html).
Converting MarkerImage objects to
type Icon
Until version 3.10 of the Google Maps JavaScript API, complex icons were
defined as MarkerImage objects. The Icon object
literal was added in version 3.10, and replaces MarkerImage from
version 3.11 onwards. Icon object literals support the same
parameters as MarkerImage, allowing you to easily convert a
MarkerImage to an Icon by removing the
constructor, wrapping the previous parameters in {}'s,
and adding the names of each parameter. For example:
var image = new google.maps.MarkerImage(
place.icon,
new google.maps.Size(71, 71),
new google.maps.Point(0, 0),
new google.maps.Point(17, 34),
new google.maps.Size(25, 25));
becomes
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
Symbols
In addition to raster images, a marker supports the display of vector paths
called Symbols. To display a vector
path, pass a Symbol object literal with the desired path to the
marker's icon property. You can use one of the predefined paths
in
google.maps.SymbolPath
or define a custom path using
SVG path notation.
For more information, see the documentation for symbols.
Make a marker draggable
To allow users to drag a marker to a different location on the map, set
draggable to true in the marker options.
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// Place a draggable marker on the map
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
draggable:true,
title:"Drag me!"
});