Skip to main content

Markers

Simple marker

To add a Marker object to the map, specify the required coordinates:

const marker = new mapgl.Marker(map, {
coordinates: [37.615655, 55.768005],
});

Multiple markers

To add multiple Marker objects to the map, use a loop:

const coords = [
[37.615655, 55.768005],
[37.608676, 55.766095],
[37.612247, 55.754607],
[37.634183, 55.754402],
[37.630123, 55.773231],
];
coords.forEach((coord) => {
const marker = new mapgl.Marker(map, {
coordinates: coord,
});
});

Marker with a custom icon

To change the marker icon, specify the icon parameter:

const marker = new mapgl.Marker(map, {
coordinates: [37.615655, 55.768005],
icon: 'https://docs.2gis.com/img/mapgl/marker.svg',
});

Marker with text

Simple text label

To add a text for a marker, specify the label parameter:

const marker = new mapgl.Marker(map, {
coordinates: [37.615655, 55.768005],
label: {
text: "Marker label",
},
});

Text position

To change the position of the text, specify the relativeAnchor and offset parameters for the label (see LabelOptions for more information):

// Right-center alignment
const rightCenter = new mapgl.Marker(map, {
coordinates: [37.615655, 55.768005],
icon: 'https://docs.2gis.com/img/mapgl/marker.svg',
label: {
text: 'Right-center alignment',
offset: [20, 0],
relativeAnchor: [0, 0.5],
},
});
// Left-top alignment
const leftTop = new mapgl.Marker(map, {
coordinates: [37.601921, 55.762252],
icon: 'https://docs.2gis.com/img/mapgl/marker.svg',
label: {
text: 'Left-top alignment',
offset: [-10, -10],
relativeAnchor: [1, 1],
},
});

Text background

To add a text label with a background image, specify the image parameter (see LabelImage for more information):

const marker = new mapgl.Marker(map, {
coordinates: [37.615655, 55.768005],
label: {
text: "Marker label",
offset: [0, 25],
relativeAnchor: [0.5, 0],
image: {
url: 'tooltip-top.svg',
size: [100, 50],
stretchX: [
[10, 40],
[60, 90],
],
stretchY: [[20, 40]],
padding: [20, 10, 10, 10],
},
},
});

For more information on how to correctly stretch the image, see label examples.

Events

To subscribe to events such as click on a marker, use the on() method (see DynamicObjectEventTable for the list of possible events):

const marker = new mapgl.Marker(map, {
coordinates: [37.615655, 55.768005],
icon: 'https://docs.2gis.com/img/mapgl/marker.svg',
});
marker.on('click', (e) => {
alert('You clicked the marker');
});