Skip to main content

Marker

The Marker object displays a single marker on the map.

Marker extends Layer.

Example usage

const map = G.map('map', { apiKey: 'my-api-key', center: { latitude: 48.864716, longitude: 2.3522 } });
map.load().then(() => {
const marker = G.marker({
latitude: 48.9,
longitude: 2.4,
map: map,
title: 'My Marker',
});
});

Creating the Marker object

G.marker(options?: MarkerOptions): Marker

There are a few ways that you can setup the Marker object.

No parameters.

G.marker(): Marker

const marker = G.marker();

Pass the marker options.

G.marker(options: MarkerOptions): Marker

ParameterTypeRequiredDescription
optionsMarkerOptionsYesThe options.
const marker = G.marker({position: [48.9, 2.4]});

Set the location only.

G.marker(position: LatLngValue): Marker

ParameterTypeRequiredDescription
positionLatLngValueYesThe latitude/longitude value.
const marker = G.marker([48.9, 2.4]);

Set the location and the options.

G.marker(position: LatLngValue, options: MarkerOptions): Marker

ParameterTypeRequiredDescription
positionLatLngValueYesThe latitude/longitude value.
optionsMarkerOptionsYesThe options.
const marker = G.marker([48.9, 2.4], {title: 'Marker title'});

Pass an existing Marker object.

G.marker(object: Marker): Marker

In this case the Marker object is simply returned.

ParameterTypeRequiredDescription
objectMarkerYesA Marker object.
const marker = G.marker(markerObject);

MarkerLabel type

The MarkerLabel type is an object that represents the label value for the marker. You can either pass a string for the label value, or an object.

The MarkerLabel type is the same as the Google Maps MarkerLabel interface.

OptionTypeDefaultDescription
classNamestringThe class attribute value for the label.
colorstringblackThe label text color
fontFamilystringThe label text font family
fontSizestring14pxThe label text font size
fontWeightstringThe label text font weight
textstringThe label text

Custom data type

You can attach an object of data to the marker object to hold some custom data. An example usage would be to include some content that you want to show in a popup when a marker is clicked.

The CustomData type represents this data. Essentially it's an object that can hold any type of data

type CustomData = {
[key: string]: any;
}

Marker options

Type MarkerOptions

OptionTypeDefaultDescription
anchorPointPointValueThe offset from the marker's position to the tip of an InfoWindow that has been opened with the marker as anchor.
cursorstringpointerThe cursor type to show on hover.
dataCustomDataAn optional object to hold custom data to attach to the marker object.
draggablebooleanfalseWhether the marker can be dragged on the map.
iconIconValueThe icon value for the marker.
labelstring or MarkerLabelThe label for the marker.
latnumberThe latitude for the marker. This is an alternate option to position and latitude. You should set this if you are setting longitude or lng.
latitudenumberThe latitude for the marker. This is an alternate option to position and lat. You should set this if you are setting longitude or lng.
lngnumberThe longitude for the marker. This is an alternate option to position and longitude. You should set this if you are setting latitude or lat.
longitudenumberThe longitude for the marker. This is an alternate option to position and lng. You should set this if you are setting latitude or lat.
mapMapThe map that the marker should show on.
positionLatLngValueThe Marker position
svgIconSvgSymbolValueThe SVG icon value for the marker. If it's a string then it's the path code for the SVG icon.
titlestringThe title for the marker. If a custom tooltip is not used, this will show as a default tooltip on the marker.
tooltipTooltipValueThe tooltip for the marker. This will show when hovering over the marker.

Events

All of the Google Map marker events can used. Below are the marker events specific to this library.

EventDescription
readyThe marker is loaded and ready for use.

Properties

  • Properties inherited from Layer.
PropertyTypeDescription
anchorPointPointValueThe offset from the marker's position to the tip of an InfoWindow that has been opened with the marker as anchor.
cursorstringThe cursor type to show on hover.
dataCustomDataAn object that holds custom data to attach to the marker object.
draggablebooleanWhether the marker can be dragged on the map.
iconIconThe icon value for the marker.
labelstring or MarkerLabelThe label for the marker.
mapMapThe map that the marker should show on.
positionLatLngThe Marker position
titlestringThe title for the marker. If a custom tooltip is not used, this will show as a default tooltip on the marker.

Methods

  • Methods inherited from Layer.
  • Methods inherited from Evented.
  • Methods inherited from Base

display

display(map: Map): Marker

Add the marker to the map and display it. This is an alias to show().

ParameterTypeRequiredDescription
mapMapYesThe map object
marker.display();

getData

getData(key?: string): any

Gets either all the custom data attached to the marker object or the value for a specific data key from the custom data object.

If the key is specified but the value doesn't exist in the custom data object, then null is returned.

ParameterTypeRequiredDescription
keystringNoThe object key to get a specific piece of data from the custom data obect. If this is not set then the entire custom data object is returned.

Get the entire data object.

const customData = marker.getData();

Get a specific value from the custom data object.

const customValue = marker.getData('dataKey');

getDraggable

getDraggable(): boolean

Returns whether the marker can be dragged on the map.

if (marker.getDraggable()) {
// Do something
}

getPosition

getPosition(): LatLng

Get the marker position

const position = marker.getPosition();

hide

hide(): Marker

Remove the marker from the map to hide it.

marker.hide();

onAnimationChanged

onAnimationChanged(callback: EventCallback): void

Callback for when the marker's animation changes.

This is a convenience function for marker.on(G.MarkerEvents.ANIMATION_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onAnimationChanged(() => {
// Do something
})

onClick

onClick(callback: EventCallback): void

Callback for when the marker icon is clicked.

This is a convenience function for marker.on(G.MarkerEvents.CLICK, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onClick(() => {
// Do something
})

onClickableChanged

onClickableChanged(callback: EventCallback): void

Callback for when the marker clickable property changes.

This is a convenience function for marker.on(G.MarkerEvents.CLICKABLE_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onClickableChanged(() => {
// Do something
})

onContextMenu

onContextMenu(callback: EventCallback): void

Callback for when the DOM contextmenu is fired on the marker.

This is a convenience function for marker.on(G.MarkerEvents.CONTEXT_MENU, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onContextMenu(() => {
// Do something
})

onCursorChanged

onCursorChanged(callback: EventCallback): void

Callback for when the marker cursor property changes.

This is a convenience function for marker.on(G.MarkerEvents.CURSOR_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onCursorChanged(() => {
// Do something
})

onDblClick

onDblClick(callback: EventCallback): void

Callback for when the arker is double clicked.

This is a convenience function for marker.on(G.MarkerEvents.DBLCLICK, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onDblClick(() => {
// Do something
})

onDrag

onDrag(callback: EventCallback): void

Callback for when the user drags the marker.

This is a convenience function for marker.on(G.MarkerEvents.DRAG, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onDrag(() => {
// Do something
})

onDragEnd

onDragEnd(callback: EventCallback): void

Callback for when the user stops dragging the marker.

This is a convenience function for marker.on(G.MarkerEvents.DRAG_END, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onDragEnd(() => {
// Do something
})

onDraggableChanged

onDraggableChanged(callback: EventCallback): void

Callback for when the marker draggable property changes.

This is a convenience function for marker.on(G.MarkerEvents.DRAGGABLE_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onDraggableChanged(() => {
// Do something
})

onDragStart

onDragStart(callback: EventCallback): void

Callback for when the user stops dragging the marker.

This is a convenience function for marker.on(G.MarkerEvents.DRAG_START, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onDragStart(() => {
// Do something
})

onFlatChanged

onFlatChanged(callback: EventCallback): void

Callback for when the marker flat property changes.

This is a convenience function for marker.on(G.MarkerEvents.FLAT_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onFlatChanged(() => {
// Do something
})

onIconChanged

onIconChanged(callback: EventCallback): void

Callback for when the marker icon property changes.

This is a convenience function for marker.on(G.MarkerEvents.ICON_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onIconChanged(() => {
// Do something
})

onMouseDown

onMouseDown(callback: EventCallback): void

Callback for when the when the user's mouse is pressed down on the marker.

This is a convenience function for marker.on(G.MarkerEvents.MOUSE_DOWN, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onMouseDown(() => {
// Do something
})

onMouseMove

onMouseMove(callback: EventCallback): void

Callback for when the when the user's mouse moves over the marker icon.

This is a convenience function for marker.on(G.MarkerEvents.MOUSE_MOVE, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onMouseMove(() => {
// Do something
})

onMouseOut

onMouseOut(callback: EventCallback): void

Callback for when the when the user's mouse exits the marker icon.

This is a convenience function for marker.on(G.MarkerEvents.MOUSE_OUT, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onMouseOut(() => {
// Do something
})

onMouseOver

onMouseOver(callback: EventCallback): void

Callback for when the when the user's mouse enters the marker icon.

This is a convenience function for marker.on(G.MarkerEvents.MOUSE_OVER, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onMouseOver(() => {
// Do something
})

onMouseUp

onMouseUp(callback: EventCallback): void

Callback for the mouseup event on the marker.

This is a convenience function for marker.on(G.MarkerEvents.MOUSE_UP, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onMouseUp(() => {
// Do something
})

onPositionChanged

onPositionChanged(callback: EventCallback): void

Callback for when the marker's position property changes.

This is a convenience function for marker.on(G.MarkerEvents.POSITION_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onPositionChanged(() => {
// Do something
})

onReady

onReady(callback: EventCallback): void

Callback for when the marker is loaded and ready for use.

This is a convenience function for marker.on(G.MarkerEvents.READY, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onReady(() => {
// Do something
})

onShapeChanged

onShapeChanged(callback: EventCallback): void

Callback for when the marker's shape property changes.

This is a convenience function for marker.on(G.MarkerEvents.SHAPE_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onShapeChanged(() => {
// Do something
})

onTitleChanged

onTitleChanged(callback: EventCallback): void

Callback for when the marker's title property changes.

This is a convenience function for marker.on(G.MarkerEvents.TITLE_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onTitleChanged(() => {
// Do something
})

onVisibleChanged

onVisibleChanged(callback: EventCallback): void

Callback for when the marker's visible property changes.

This is a convenience function for marker.on(G.MarkerEvents.VISIBLE_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onVisibleChanged(() => {
// Do something
})

onZIndexChanged

onZIndexChanged(callback: EventCallback): void

Callback for when the marker's zindex property changes.

This is a convenience function for marker.on(G.MarkerEvents.ZINDEX_CHANGED, callback).

ParameterTypeRequiredDescription
callbackFunctionYesThe callback function that will be called when the event is dispatched.
marker.onZIndexChanged(() => {
// Do something
})

setAnchorPoint

setAnchorPoint(value: PointValue): Promise<Marker>

Set the anchor point for the marker.

ParameterTypeRequiredDescription
valuePointValueYesThe offset from the marker's position to the tip of an InfoWindow that has been opened with the marker as anchor.
marker.setAnchorPoint([3, 10]);

setAnchorPointSync

setAnchorPoint(value: PointValue): Promise<Marker>

Set the anchor point for the marker syncronously.

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setAnchorPoint() instead or pass the anchor option to the constructor or setOptions().

ParameterTypeRequiredDescription
valuePointValueYesThe offset from the marker's position to the tip of an InfoWindow that has been opened with the marker as anchor.
marker.setAnchorPointSync([3, 10]);

setCursor

setCursor(value: string): Promise<Marker>

Set the mouse cursor type to show when hovering over the marker.

ParameterTypeRequiredDescription
valuestringYesThe mouse cursor type.
marker.setCursor('help');

setCursorSync

setCursorSync(value: string): Marker

Syncronously set the mouse cursor type to show when hovering over the marker.

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setCursor() instead or pass the cursor option to the constructor or setOptions().

ParameterTypeRequiredDescription
valuestringYesThe mouse cursor type.
marker.setCursorSync('help');

setDraggable

setDraggable(value: boolean): Promise<Marker>

Set whether the marker can be dragged on the map.

ParameterTypeRequiredDescription
valuebooleanYesWhether the marker can be dragged on the map.
marker.setDraggable(true);

setDraggableSync

setDraggableSync(value: boolean): Marker

Syncronously set whether the marker can be dragged on the map.

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setDraggable() instead or pass the draggable option to the constructor or setOptions().

ParameterTypeRequiredDescription
valuebooleanYesWhether the marker can be dragged on the map.
marker.setDraggableSync(true);

setIcon

setIcon(value: Icon | SvgSymbol | string): Promise<Marker>

Set the icon value for the marker.

ParameterTypeRequiredDescription
valueIcon, SvgSymbol, or stringYesThe icon for the marker.

If value is a string then it should be a URL to the image to use for the icon.

Set the icon using an Icon object:

const icon = G.icon({
url: 'https://mywebsite.com/images/marker.png',
size: [20, 32]
});
marker.setIcon(icon);

Set the icon using an SvgSymbol object:

const icon = G.svgSymbol({
path: 'M0,6a6,6 0 1,0 12,0a6,6 0 1,0 -12,0',
fillColor: '#5284ed',
fillOpacity: 1,
scale: 1,
strokeColor: '#5284ed',
strokeOpacity: 0.5,
strokeWeight: 4,
});
marker.setIcon(icon);

Set the icon value using a URL string:

marker.setIcon('https://mywebsite.com/images/marker.png');

setIconSync

setIconSync(value: Icon | SvgSymbol | string): Marker

Syncronously set the icon value for the marker.

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setIcon() instead or pass the icon option to the constructor or setOptions().

ParameterTypeRequiredDescription
valueIcon, SvgSymbol, or stringYesThe icon for the marker.

If value is a string then it should be a URL to the image to use for the icon.

Set the icon using an Icon object:

const icon = G.icon({
url: 'https://mywebsite.com/images/marker.png',
size: [20, 32]
});
marker.setIconSync(icon);

Set the icon using an SvgSymbol object:

const icon = G.svgSymbol({
path: 'M0,6a6,6 0 1,0 12,0a6,6 0 1,0 -12,0',
fillColor: '#5284ed',
fillOpacity: 1,
scale: 1,
strokeColor: '#5284ed',
strokeOpacity: 0.5,
strokeWeight: 4,
});
marker.setIconSync(icon);

Set the icon value using a URL string:

marker.setIconSync('https://mywebsite.com/images/marker.png');

setLabel

setLabel(value: string | number | MarkerLabel): Promise<Marker>

Set the label value for the marker.

ParameterTypeRequiredDescription
valuestring, number, or MarkerLabelYesThe label for the marker.

Set the label as a string:

marker.setLabel('Marker label');

Set the label as a number:

marker.setLabel(15);

Set the label as a MarkerLabel object:

marker.setLabel({
className: 'myLabelClass',
fontSize: '18px',
text: 'Label text here'
});

setLabelSync

setLabelSync(value: string | number | MarkerLabel): Marker

Syncronously set the label value for the marker.

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setLabel() instead or pass the label option to the constructor or setOptions().

ParameterTypeRequiredDescription
valuestring, number, or MarkerLabelYesThe label for the marker.

Set the label as a string:

marker.setLabelSync('Marker label');

Set the label as a number:

marker.setLabelSync(15);

Set the label as a MarkerLabel object:

marker.setLabelSync({
className: 'myLabelClass',
fontSize: '18px',
text: 'Label text here'
});

setMap

setMap(map: Map): Promise<Marker>

Add the marker to the map and display it. This is an alias to show().

ParameterTypeRequiredDescription
mapMapYesThe map object
marker.setMap(map);

setMapSync

setMapSync(map: Map): Marker

Syncronously add the marker to the map and display it. This is an alias to show().

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setMap() instead or pass the map option to the constructor or setOptions().

ParameterTypeRequiredDescription
mapMapYesThe map object
marker.setMapSync(map);

setOptions

setOptions(options: MarkerOptions): Marker

Set the options for the marker.

ParameterTypeRequiredDescription
optionsMarkerOptionsYesThe marker options.
marker.setOptions({position: [38.6270, 90.1994]});

setPosition

setPosition(value: LatLngValue): Promise<Marker>

Set the position of the marker.

ParameterTypeRequiredDescription
valueLatLngValueYesThe marker position.
marker.setPosition([49.864716, 2.6522]);

setPositionSync

setPositionSync(value: LatLngValue): Marker

Syncronously set the position of the marker.

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setPosition() instead or pass the position option to the constructor or setOptions().

ParameterTypeRequiredDescription
valueLatLngValueYesThe marker position.
marker.setPositionSync([49.864716, 2.6522]);

setTitle

setTitle(value: string): Promise<Marker>

Set the title for the marker.

ParameterTypeRequiredDescription
valuestringYesThe marker title.
marker.setTitle('Marker title');

setTitleSync

setTitleSync(value: string): Marker

Syncronously set the title of the marker.

Only use this if you know that the Google Maps library is already loaded and you have to set up the marker syncronously. If you don't have to set up the marker syncronously, then use setTitle() instead or pass the title option to the constructor or setOptions().

ParameterTypeRequiredDescription
valuestringYesThe marker title.
marker.setTitleSync('Marker title');

show

show(map: Map): Promise<Marker>

Add the marker to the map and display it. This is an alias to setMap.

ParameterTypeRequiredDescription
mapMapYesThe map object
marker.show(map);

toGoogle

toGoogle(): Promise<google.maps.Marker>

Returns the Google Maps Marker object. It will be a promise value.

marker.toGoogle().then((googleMarker) => {
// Do something with the Google marker object
});

toGoogleSync

toGoogleSync(): google.maps.Marker

Syncronously returns the Google Maps Marker object.

This is different from toGoogle() because it will throw an error if the Google Maps library is not available, whereas toGoogle() will wait for the Google Maps library to load. (This is the same with all of the Sync versions of the marker methods.)

Only use this when you have to get the Google Maps object synchronously and you know that the Google Maps library is already loaded. If you don't have to get the Google Maps object synchronously, then use toGoogle() instead.

const googleMarker = marker.toGoogleSync();