User Geolocation
Show the user's current GPS position on the map. Each library takes its own approach, compared side by side below.
Quick recommendations
Leaflet: Makes the most sense when you want manual control over the geolocation UX with a custom accuracy circle and marker using the browser Geolocation API.
MapLibre GL JS: A strong fit when you want a built-in geolocate button with automatic tracking, heading indicator, and camera follow mode.
OpenLayers: Best when you need geolocation integrated with OpenLayers' coordinate projection system and feature-based position rendering.
MapTiler SDK: Ideal for the simplest setup, because the built-in GeolocateControl works identically to MapLibre's with zero extra configuration.
At a glance
| Leaflet | MapLibre | OpenLayers | SDK | |
|---|---|---|---|---|
| Coordinates | [lat, lng] | [lng, lat] | fromLonLat([lng, lat]) | [lng, lat] |
| Rendering | DOM + Canvas (2D) | WebGL (GPU, 60fps) | Canvas + WebGL | WebGL (GPU, 60fps) |
| Bundle size | ~40 KB gzipped | ~300 KB gzipped | ~90 KB gzipped (tree-shakeable) | ~360 KB gzipped (includes MapLibre) |
| Learning curve | Easiest (simple API, great tutorials) | Moderate (source/layer model, expressions) | Steepest (projections, OGC standards, verbose API) | Easy (MapLibre API with convenience helpers) |
| 3D support | No native 3D (2D only) | Full: terrain, fill-extrusion, globe, sky | Limited: WebGL layers, no native globe | Full (same as MapLibre) |
| Code lines | 13 | 14 | 27 | 12 |
Code snippets
Click a library to expand its code. Each snippet shows only the core logic: the scaffold handles HTML boilerplate.
Leaflet 13 lines Show code
const map = L.map('map').setView([50.0755, 14.4378], 5);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
tileSize: 256,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
map.locate({ setView: true, maxZoom: 16 });
map.on('locationfound', (e) => {
L.marker(e.latlng).addTo(map).bindPopup('You are here').openPopup();
L.circle(e.latlng, { radius: e.accuracy, color: '#2196F3', fillOpacity: 0.15 }).addTo(map);
});
map.on('locationerror', (e) => {
alert('Location access denied or unavailable.');
}); MapLibre GL JS 14 lines Show code
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 12
});
map.addControl(new maplibregl.NavigationControl());
const geolocate = new maplibregl.GeolocateControl({
positionOptions: { enableHighAccuracy: true },
trackUserLocation: true,
showUserHeading: true
});
map.addControl(geolocate);
map.on('load', () => { geolocate.trigger(); }); OpenLayers 27 lines Show code
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 2 })
});
var positionFeature = new ol.Feature();
positionFeature.setStyle(new ol.style.Style({
image: new ol.style.Circle({ radius: 8, fill: new ol.style.Fill({ color: '#2196F3' }), stroke: new ol.style.Stroke({ color: '#fff', width: 2 }) })
}));
var accuracyFeature = new ol.Feature();
var locationLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [accuracyFeature, positionFeature] }) });
map.addLayer(locationLayer);
var geolocation = new ol.Geolocation({
trackingOptions: { enableHighAccuracy: true },
projection: map.getView().getProjection(),
tracking: true
});
geolocation.on('change:position', function() {
var coords = geolocation.getPosition();
positionFeature.setGeometry(coords ? new ol.geom.Point(coords) : null);
map.getView().animate({ center: coords, zoom: 15 });
});
geolocation.on('change:accuracyGeometry', function() {
accuracyFeature.setGeometry(geolocation.getAccuracyGeometry());
}); MapTiler SDK 12 lines Show code
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
})); Things to know
Implementation-specific details that matter for user geolocation.
Uses the raw browser Geolocation API, so you handle watchPosition, accuracy circle rendering, and error states manually.
GeolocateControl provides a one-line solution with tracking, accuracy circle, and fly-to animation; no manual Geolocation API needed.
Uses ol.Geolocation which wraps the browser API and emits position as ol.Coordinate in the map's projection (EPSG:3857).
GeolocateControl is identical to MapLibre's; the SDK adds no extra geolocation features beyond the inherited control.
Which library to use?
Locate the user on a Leaflet map with map.locate(), then mark the position with L.marker() and an L.circle() sized to the GPS accuracy.
Makes the most sense when you want manual control over the geolocation UX with a custom accuracy circle and marker using the browser Geolocation API.
Track the user's position in MapLibre GL JS with maplibregl.GeolocateControl, setting trackUserLocation and calling trigger() on load.
A strong fit when you want a built-in geolocate button with automatic tracking, heading indicator, and camera follow mode.
Follow the user's position in OpenLayers with ol.Geolocation, drawing the location and its accuracy circle as features on an ol.layer.Vector.
Best when you need geolocation integrated with OpenLayers' coordinate projection system and feature-based position rendering.
Track the user's position on a MapTiler SDK map with maptilersdk.GeolocateControl, using trackUserLocation and showUserHeading for live updates.
Ideal for the simplest setup, because the built-in GeolocateControl works identically to MapLibre's with zero extra configuration.
Build maps with AI
Generate code like these examples using mapping library skills in your IDE.