Get Current Location using addGeolocateControls

This documentation provides methods for getting, tracking users current location using addGeolocateControls
This provides a button that uses the browser's geolocation API to locate the user on the map.
This has two modes. If trackUserLocation is false (default) the control acts as a button, which when pressed will set the map's camera to target the user location. If the user moves, the map won't update. This is most suited for the desktop. If trackUserLocation is true the control acts as a toggle button that when active the user's location is actively monitored for changes. In this mode the GeolocateControl has three interaction states: * active - the map's camera automatically updates as the user's location changes, keeping the location dot in the center. Initial state and upon clicking the GeolocateControl button. * passive - the user's location dot automatically updates, but the map's camera does not. Occurs upon the user initiating a map movement. * disabled - occurs if Geolocation is not available, disabled or denied.

Example

Use addGeolocateControls to set users location
const geolocate = olaMaps.addGeolocateControls({
  positionOptions: {
    enableHighAccuracy: true,
  },
  trackUserLocation: true,
})

myMap.addControl(geolocate)
Programmatically request and move the map to the user's location.
myMap.on('load', () => {
  geolocate.trigger()
})

Events

geolocate
This event is triggered when the user's location is successfully retrieved.
geolocate.on('geolocate', (event) => {
  console.log('A geolocate event has occurred')
})
error
This event is emitted when there is an error in retrieving the user's location.
geolocate.on('error', () => {
  console.log('An error event has occurred.')
})
trackuserlocationstart
Triggered when tracking of the user's location begins.
geolocate.on('trackuserlocationstart', () => {
  console.log('User location tracking has started.');
})
trackuserlocationend
Triggered when tracking of the user's location ends.
geolocate.on('trackuserlocationend', () => {
  console.log('User location tracking has ended.');
})
userlocationfocus
Triggered when the user's location gains focus, such as when a map is centered on it.
geolocate.on('userlocationfocus', () => {
  console.log('User location is focused on the map.');
})
userlocationlostfocus
Triggered when the user's location loses focus, such as when the map is panned away.
geolocate.on('userlocationlostfocus', () => {
  console.log('User location has lost focus on the map.');
})
outofmaxbounds
Triggered when the user's location is outside the defined maximum bounds.
geolocate.on('outofmaxbounds', () => {
  console.warn('User location is out of the maximum bounds.');
})