Markers (Legacy) _ Maps JavaScript API _ Google for Developers
Markers (Legacy) _ Maps JavaScript API _ Google for Developers
Markers (Legacy)
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 (/maps/documentation/javascript/reference/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 (#icons).
Broadly speaking, markers are a type of overlay. For information on other types of overlay, see
Drawing on the map (/maps/documentation/javascript/overlays).
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
(/maps/documentation/javascript/infowindows#open) 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 (#draggable).
Add a marker
The following fields are particularly important and commonly set when constructing a marker:
https://developers.google.com/maps/documentation/javascript/markers 1/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
position (required) specifies a LatLng identifying the initial location of the marker. One way
of retrieving a LatLng is by using the Geocoding service
(/maps/documentation/javascript/geocoding).
map (optional) specifies the Map on 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's setMap() method.
The following example adds a simple marker to a map at Uluru, in the center of Australia:
TypeScript (#typescript)JavaScript
(#javascript)
function initMap() {
const myLatLng = { lat: -25.363, lng: 131.044 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: myLatLng,
});
new google.maps.Marker({
position: myLatLng,
map,
title: "Hello World!",
});
}
window.initMap = initMap;
ob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/marker-simple/docs/index.js#L7-L21)
Try Sample
JSFiddle.net…
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:
https://developers.google.com/maps/documentation/javascript/markers 2/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
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.
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 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.
https://developers.google.com/maps/documentation/javascript/markers 3/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
You can customize the visual appearance of markers by specifying an image file (#simple_icons) or
vector-based icon (#symbols) to display instead of the default Google Maps pushpin icon. You can
add text with a marker label (#marker_labels), and use complex icons (#complex_icons) to define
clickable regions, and set the stack order of markers.
TypeScript (#typescript)JavaScript
(#javascript)
// This example adds a marker to indicate the position of Bondi Beach in Sydney,
// Australia.
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: { lat: -33, lng: 151 },
});
const image =
"https://developers.google.com/maps/documentation/javascript/examples/full/image
const beachMarker = new google.maps.Marker({
position: { lat: -33.89, lng: 151.274 },
map,
icon: image,
});
}
window.initMap = initMap;
s/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/icon-simple/docs/index.js#L7-L23)
Try Sample
https://developers.google.com/maps/documentation/javascript/markers 4/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
JSFiddle.net…
You can use custom SVG vector paths to define the visual appearance of markers. To do this,
pass a Symbol object literal with the desired path to the marker's icon property. You can define a
custom path using SVG path notation (http://www.w3.org/TR/SVG/paths.html#PathData), or use one of
the predefined paths in google.maps.SymbolPath
(/maps/documentation/javascript/reference#SymbolPath). The anchor property is required in order for
the marker to render correctly when the zoom level changes. Learn more about using Symbols to
create vector-based icons (/maps/documentation/javascript/symbols) for markers (and polylines).
TypeScript (#typescript)JavaScript
(#javascript)
new google.maps.Marker({
position: map.getCenter(),
icon: svgMarker,
map: map,
});
}
window.initMap = initMap;
f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/marker-symbol-custom/docs/index.js#L7-L33)
https://developers.google.com/maps/documentation/javascript/markers 5/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
Try Sample
JSFiddle.net…
Marker labels
A marker label is a letter or number that appears inside a marker. The marker image in
this section displays a marker label with the letter 'B' on it. You can specify a marker label
as either a string or a MarkerLabel
(/maps/documentation/javascript/3.exp/reference#MarkerLabel) object that includes a string and
other label properties.
When creating a marker, you can specify a label property in the MarkerOptions
(/maps/documentation/javascript/3.exp/reference#MarkerOptions) object. Alternatively, you can call
setLabel() on the Marker (/maps/documentation/javascript/3.exp/reference#Marker) object to set the
label on an existing marker.
The following example displays labeled markers when the user clicks on the map:
TypeScript (#typescript)JavaScript
(#javascript)
// In the following example, markers appear when the user clicks on the map.
// Each marker is labeled with a single alphabetical character.
const labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let labelIndex = 0;
function initMap() {
const bangalore = { lat: 12.97, lng: 77.59 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: bangalore,
});
https://developers.google.com/maps/documentation/javascript/markers 6/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
window.initMap = initMap;
lob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/marker-labels/docs/index.js#L7-L38)
Try Sample
JSFiddle.net…
Complex icons
You can 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
(/maps/documentation/javascript/3.exp/reference#Icon).
If you are using a label (#marker_labels) with a custom marker, you can position the label with the
labelOrigin property in the Icon (/maps/documentation/javascript/3.exp/reference#Icon) object.
https://developers.google.com/maps/documentation/javascript/markers 7/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
Note: Marker shadows were removed in version 3.14 of the Maps JavaScript API. Any shadows specified
programmatically will be ignored.
TypeScript (#typescript)JavaScript
(#javascript)
setMarkers(map);
}
// 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.
const 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) {
// Adds 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.
const image = {
url: "https://developers.google.com/maps/documentation/javascript/examples/full/
// This marker is 20 pixels wide by 32 pixels high.
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.
const shape = {
coords: [1, 1, 1, 20, 18, 20, 18, 1],
type: "poly",
https://developers.google.com/maps/documentation/javascript/markers 8/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
};
new google.maps.Marker({
position: { lat: beach[1], lng: beach[2] },
map,
icon: image,
shape: shape,
title: beach[0],
zIndex: beach[3],
});
}
}
window.initMap = initMap;
blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/icon-complex/docs/index.js#L7-L66)
Try Sample
JSFiddle.net…
Until version 3.10 of the 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:
https://developers.google.com/maps/documentation/javascript/markers 9/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
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)
};
Optimize markers
Optimization enhances performance by rendering many markers as a single static element. This
is useful in cases where a large number of markers is required. By default, the Maps JavaScript
API will decide whether a marker will be optimized. When there is a large number of markers, the
Maps JavaScript API will attempt to render markers with optimization. Not all Markers can be
optimized; in some situations, the Maps JavaScript API may need to render Markers without
optimization. Disable optimized rendering for animated GIFs or PNGs, or when each marker must
be rendered as a separate DOM element. The following example shows creating an optimized
marker:
optimized: true
https://developers.google.com/maps/documentation/javascript/markers 10/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
You can make a marker accessible by adding a click listener event, and setting optimized to
false. The click listener causes the marker to have button semantics, which can be accessed
using keyboard navigation, screen readers, and so on. Use the title option to present
accessible text for a marker.
In the following example, the first marker receives focus when tab is pressed; you can then use
the arrow keys to move between markers. Press tab again to continue moving through the rest of
the map controls. If a marker has an info window, you can open it by clicking the marker, or by
pressing the enter key or space bar when the marker is selected. When the info window closes,
focus will return to the associated marker.
TypeScript (#typescript)JavaScript
(#javascript)
// Add a click listener for each marker, and set up the info window.
marker.addListener("click", () => {
infoWindow.close();
infoWindow.setContent(marker.getTitle());
infoWindow.open(marker.getMap(), marker);
https://developers.google.com/maps/documentation/javascript/markers 11/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
});
});
}
window.initMap = initMap;
83f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/marker-accessibility/docs/index.js#L7-L46)
Try Sample
JSFiddle.net…
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:
DROP indicates 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 and animation
will revert to null. This type of animation is usually specified during creation of the Marker.
BOUNCE indicates that the marker should bounce in place. A bouncing marker will continue
bouncing until its animation property is explicitly set to null.
You may initiate an animation on an existing marker by calling setAnimation() on the Marker
object.
TypeScript (#typescript)JavaScript
(#javascript)
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
https://developers.google.com/maps/documentation/javascript/markers 12/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
zoom: 13,
center: { lat: 59.325, lng: 18.07 },
});
function toggleBounce() {
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
window.initMap = initMap;
683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/marker-animations/docs/index.js#L7-L35)
Try Sample
JSFiddle.net…
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);
https://developers.google.com/maps/documentation/javascript/markers 13/14
2025. 05. 22. 12:49 Markers (Legacy) | Maps JavaScript API | Google for Developers
}
}
To allow users to drag a marker to a different location on the map, set draggable to true in the
marker options.
For further extensions of the Marker class, marker clustering and management, and overlay
customization, see open source libraries
(/maps/documentation/javascript/libraries-open-source#extending_markers_and_overlays).
https://developers.google.com/maps/documentation/javascript/markers 14/14