MapSnippets logo MapSnippets logo

Geolocation Control with MapTiler SDK

MapTiler SDK

Track the user's position on a MapTiler SDK map with maptilersdk.GeolocateControl, using trackUserLocation and showUserHeading for live updates.

progress_activity Loading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [15.0, 50.0],
  zoom: 5
});
map.addControl(new maptilersdk.GeolocateControl({
  positionOptions: { enableHighAccuracy: true },
  trackUserLocation: true,
  showUserHeading: true
}));

How to show the user’s location with MapTiler SDK

The GeolocateControl adds a button that requests the browser’s geolocation permission. Once granted, it shows a pulsing dot at the user’s position and optionally tracks movement.

Setting trackUserLocation: true keeps the dot updated as the user moves. The showUserHeading option adds a directional cone showing which way the device is facing (on supported devices).

The control uses the browser’s Geolocation API, so it works on both desktop and mobile.

Key APIs

API Description Docs
maptilersdk.GeolocateControl Built-in geolocation button and tracking docs.maptiler.com/sdk-js/api/
trackUserLocation Continuously update position docs.maptiler.com/sdk-js/api/
showUserHeading Show device heading direction docs.maptiler.com/sdk-js/api/

FAQ

What happens if the user denies location permission?

The control shows an error state. You can listen to the error event on the control to display a custom message.

Does this work over HTTP?

No. The Geolocation API requires HTTPS (or localhost). On HTTP, the browser blocks the request silently.

Common mistakes

  • Testing over HTTP, since geolocation requires HTTPS
  • Expecting automatic activation. The user must click the control button to trigger the permission prompt
  • Not setting enableHighAccuracy. On mobile, this option uses GPS instead of cell tower approximation

Build maps using MapTiler SDK with AI

Generate code like this geolocation control with maptiler sdk example using the MapTiler SDK skill in your IDE.

Related examples