MapSnippets logo MapSnippets logo

Geolocation with OpenLayers

OpenLayers

Follow the user's position in OpenLayers with ol.Geolocation, drawing the location and its accuracy circle as features on an ol.layer.Vector.

progress_activity Loading map...
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: '&copy; <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());
});

How to show the user’s location in OpenLayers

OpenLayers has a built-in ol.Geolocation class that wraps the browser’s Geolocation API. Setting tracking: true starts watching the user’s position.

Two features visualize the location: a circle for the position and a polygon for the accuracy radius. The change:position event fires when new coordinates are available, and the map animates to center on the user.

With the projection option set, coordinates come back in the map’s projection (EPSG:3857), so no manual conversion is needed.

Key APIs

API Description Docs
ol.Geolocation Browser geolocation wrapper openlayers.org
getPosition() Get current position in map projection openlayers.org
getAccuracyGeometry() Get accuracy circle as polygon openlayers.org

FAQ

What happens if the user denies location permission?

The ol.Geolocation object fires an error event. Listen for it with geolocation.on('error', handler) and show a fallback message.

Does this work on mobile devices?

Yes. Mobile browsers provide GPS-based positions with higher accuracy. Set enableHighAccuracy: true to prefer GPS over network-based positioning.

Common mistakes

  • Forgetting to set projection on the Geolocation instance, which leaves coordinates in WGS84 instead of the map’s projection
  • Not handling the error event, so the code fails silently if permission is denied
  • Animating on every position update in tracking mode, which can be disorienting. Consider centering only on the first fix.

Build maps using OpenLayers with AI

Generate code like this geolocation with openlayers example using the OpenLayers skill in your IDE.

Related examples