Moving marker on click

This example demonstrates how to reposition a marker by clicking on the map. By utilizing the map’s click event, we can dynamically update the marker’s location to match the exact coordinates where the user has clicked. This interactive functionality allows for seamless marker placement and movement across the map.

Click on the map to move the marker.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
  container: 'map', // container's id or the HTML element to render the map
  style: MapStyle.STREETS,
  center: [12.550343, 55.665957], // starting position [lng, lat]
  zoom: 9, // starting zoom
});
const marker = new maptilersdk.Marker()
  .setLngLat([lng, lat])
  .addTo(map);

map.on('click', (evt) => {
  marker.setLngLat(evt.lngLat);
});
Display simple marker on the map

Display marker

Tutorials

Learn how to add a default marker to your map.

Add an icon to the map

Add an icon to the map

Examples

Add icons from external URLs to symbol map layers.

Attach a popup to a marker instance

Attach a popup to a marker instance

Examples

Attach and display a popup to a map marker.

How to create non-colliding marker overlays

Filtered Marker Layout

Examples

Create non-colliding marker overlays to display the information from the city and town label layers.

Was this helpful?