---
title: "GeolocateControlMLGL | JavaScript maps SDK"
description: "Documentation for MapTiler SDK"
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Class GeolocateControlMLGL

A `GeolocateControl` control provides a button that uses the browser's geolocation API to locate the user on the map.

Not all browsers support geolocation, and some users may disable the feature. Geolocation support for modern browsers including Chrome requires sites to be served over HTTPS. If geolocation support is not available, the `GeolocateControl` will show as disabled.

The zoom level applied will depend on the accuracy of the geolocation provided by the device.

The `GeolocateControl` 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.

These interaction states can't be controlled programmatically, rather they are set based on user interactions.

## State Diagram[](#state-diagram)

![GeolocateControl state diagram](https://docs.maptiler.com/assets/tsdocs/3269297/78e720e5-d781-4da8-9803-a7a0e6aaaa9f)

#### Example[](#example)

```typescript
map.addControl(new GeolocateControl({
    positionOptions: {
        enableHighAccuracy: true
    },
    trackUserLocation: true
}));
```

#### See[](#see)

[Locate the user](https://maplibre.org/maplibre-gl-js/docs/examples/locate-the-user/)

## Events[](#events)

**Event** `trackuserlocationend` of type [Event](https://docs.maptiler.com/sdk-js/api-reference/classes/Event/) will be fired when the `GeolocateControl` changes to the background state, which happens when a user changes the camera during an active position lock. This only applies when `trackUserLocation` is `true`. In the background state, the dot on the map will update with location updates but the camera will not.

**Event** `trackuserlocationstart` of type [Event](https://docs.maptiler.com/sdk-js/api-reference/classes/Event/) will be fired when the `GeolocateControl` changes to the active lock state, which happens either upon first obtaining a successful Geolocation API position for the user (a `geolocate` event will follow), or the user clicks the geolocate button when in the background state which uses the last known position to recenter the map and enter active lock state (no `geolocate` event will follow unless the users's location changes).

**Event** `userlocationlostfocus` of type [Event](https://docs.maptiler.com/sdk-js/api-reference/classes/Event/) will be fired when the `GeolocateControl` changes to the background state, which happens when a user changes the camera during an active position lock. This only applies when `trackUserLocation` is `true`. In the background state, the dot on the map will update with location updates but the camera will not.

**Event** `userlocationfocus` of type [Event](https://docs.maptiler.com/sdk-js/api-reference/classes/Event/) will be fired when the `GeolocateControl` changes to the active lock state, which happens upon the user clicks the geolocate button when in the background state which uses the last known position to recenter the map and enter active lock state.

**Event** `geolocate` of type [Event](https://docs.maptiler.com/sdk-js/api-reference/classes/Event/) will be fired on each Geolocation API position update which returned as success. `data` - The returned [Position](https://developer.mozilla.org/en-US/docs/Web/API/Position) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) or [Geolocation.watchPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).

**Event** `error` of type [Event](https://docs.maptiler.com/sdk-js/api-reference/classes/Event/) will be fired on each Geolocation API position update which returned as an error. `data` - The returned [PositionError](https://developer.mozilla.org/en-US/docs/Web/API/PositionError) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) or [Geolocation.watchPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).

**Event** `outofmaxbounds` of type [Event](https://docs.maptiler.com/sdk-js/api-reference/classes/Event/) will be fired on each Geolocation API position update which returned as success but user position is out of map `maxBounds`. `data` - The returned [Position](https://developer.mozilla.org/en-US/docs/Web/API/Position) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) or [Geolocation.watchPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).

#### Example[](#example-1)

```typescript
// Initialize the geolocate control.
let geolocate = new GeolocateControl({
  positionOptions: {
      enableHighAccuracy: true
  },
  trackUserLocation: true
});
// Add the control to the map.
map.addControl(geolocate);
// Set an event listener that fires
// when a trackuserlocationend event occurs.
geolocate.on('trackuserlocationend', () => {
  console.log('A trackuserlocationend event has occurred.')
});
```

#### Example[](#example-2)

```typescript
// Initialize the geolocate control.
let geolocate = new GeolocateControl({
  positionOptions: {
      enableHighAccuracy: true
  },
  trackUserLocation: true
});
// Add the control to the map.
map.addControl(geolocate);
// Set an event listener that fires
// when a trackuserlocationstart event occurs.
geolocate.on('trackuserlocationstart', () => {
  console.log('A trackuserlocationstart event has occurred.')
});
```

#### Example[](#example-3)

```typescript
// Initialize the geolocate control.
let geolocate = new GeolocateControl({
  positionOptions: {
      enableHighAccuracy: true
  },
  trackUserLocation: true
});
// Add the control to the map.
map.addControl(geolocate);
// Set an event listener that fires
// when an userlocationlostfocus event occurs.
geolocate.on('userlocationlostfocus', function() {
  console.log('An userlocationlostfocus event has occurred.')
});
```

#### Example[](#example-4)

```typescript
// Initialize the geolocate control.
let geolocate = new GeolocateControl({
  positionOptions: {
      enableHighAccuracy: true
  },
  trackUserLocation: true
});
// Add the control to the map.
map.addControl(geolocate);
// Set an event listener that fires
// when an userlocationfocus event occurs.
geolocate.on('userlocationfocus', function() {
  console.log('An userlocationfocus event has occurred.')
});
```

#### Example[](#example-5)

```typescript
// Initialize the geolocate control.
let geolocate = new GeolocateControl({
  positionOptions: {
      enableHighAccuracy: true
  },
  trackUserLocation: true
});
// Add the control to the map.
map.addControl(geolocate);
// Set an event listener that fires
// when a geolocate event occurs.
geolocate.on('geolocate', () => {
  console.log('A geolocate event has occurred.')
});
```

#### Example[](#example-6)

```typescript
// Initialize the geolocate control.
let geolocate = new GeolocateControl({
  positionOptions: {
      enableHighAccuracy: true
  },
  trackUserLocation: true
});
// Add the control to the map.
map.addControl(geolocate);
// Set an event listener that fires
// when an error event occurs.
geolocate.on('error', () => {
  console.log('An error event has occurred.')
});
```

#### Example[](#example-7)

```typescript
// Initialize the geolocate control.
let geolocate = new GeolocateControl({
  positionOptions: {
      enableHighAccuracy: true
  },
  trackUserLocation: true
});
// Add the control to the map.
map.addControl(geolocate);
// Set an event listener that fires
// when an outofmaxbounds event occurs.
geolocate.on('outofmaxbounds', () => {
  console.log('An outofmaxbounds event has occurred.')
});
```

#### Hierarchy ([View Summary](https://docs.maptiler.com/sdk-js/api-reference/hierarchy/#GeolocateControlMLGL))

-   [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/)<[GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\>
    -   GeolocateControlMLGL
        -   [MaptilerGeolocateControl](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/)

#### Implements

-   [IControl](https://docs.maptiler.com/sdk-js/api-reference/interfaces/IControl/)

##### Index

### Constructors

[constructor](#constructor)

### Properties

[options](#options)

### Methods

[fire](#fire) [listens](#listens) [off](#off) [on](#on) [onAdd](#onadd) [once](#once) [onRemove](#onremove) [setEventedParent](#seteventedparent) [trigger](#trigger)

## Constructors

### constructor[](#constructor)

-   new GeolocateControlMLGL(options: [GeolocateControlOptions](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlOptions/)): GeolocateControlMLGL[](#constructorgeolocatecontrolmlgl)
    
    #### Parameters
    
    -   options: [GeolocateControlOptions](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlOptions/)
        
        the control's options
        
    
    #### Returns GeolocateControlMLGL
    
    Overrides [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[constructor](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#constructor)
    

## Properties

### options[](#options)

options: [GeolocateControlOptions](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlOptions/)

## Methods

### fire[](#fire)

-   fire(event: [GeolocateEvent](https://docs.maptiler.com/sdk-js/api-reference/classes/GeolocateEvent/) | [GeolocatePositionEvent](https://docs.maptiler.com/sdk-js/api-reference/classes/GeolocatePositionEvent/) | [GeolocateErrorEvent](https://docs.maptiler.com/sdk-js/api-reference/classes/GeolocateErrorEvent/)): this[](#fire-1)
    
    Calls every listener registered for the event's type.
    
    #### Parameters
    
    -   event: [GeolocateEvent](https://docs.maptiler.com/sdk-js/api-reference/classes/GeolocateEvent/) | [GeolocatePositionEvent](https://docs.maptiler.com/sdk-js/api-reference/classes/GeolocatePositionEvent/) | [GeolocateErrorEvent](https://docs.maptiler.com/sdk-js/api-reference/classes/GeolocateErrorEvent/)
    
    #### Returns this
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[fire](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#fire)
    
-   fire(type: keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/), properties?: object): this[](#fire-2)
    
    Compatibility with the (type: string, properties: Object) signature from previous versions. See [https://github.com/mapbox/mapbox-gl-js/issues/6522](https://github.com/mapbox/mapbox-gl-js/issues/6522), [https://github.com/mapbox/mapbox-gl-draw/issues/766](https://github.com/mapbox/mapbox-gl-draw/issues/766)
    
    #### Parameters
    
    -   type: keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)
    -   `Optional`properties: object
    
    #### Returns this
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[fire](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#fire)
    

### listens[](#listens)

-   listens(type: keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)): boolean[](#listens-1)
    
    Returns a true if this instance of Evented or any forwardeed instances of Evented have a listener for the specified type.
    
    #### Parameters
    
    -   type: keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)
        
        The event type
        
    
    #### Returns boolean
    
    `true` if there is at least one registered listener for specified event type, `false` otherwise
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[listens](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#listens)
    

### off[](#off)

-   off<[T](#offt) extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\>(  
        type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#offt),  
        listener: (event: [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#offt)\]) \=> void,  
    ): this[](#off-1)
    
    Removes a previously registered event listener.
    
    #### Type Parameters
    
    -   T extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)
    
    #### Parameters
    
    -   type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#offt)
        
        The event type to remove listeners for.
        
    -   listener: (event: [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#offt)\]) \=> void
        
        The listener function to remove.
        
    
    #### Returns this
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[off](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#off)
    

### on[](#on)

-   on<[T](#ont) extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\>(  
        type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#ont),  
        listener: (event: [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#ont)\]) \=> void,  
    ): [Subscription](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Subscription/)[](#on-1)
    
    Adds a listener to a specified event type.
    
    #### Type Parameters
    
    -   T extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)
    
    #### Parameters
    
    -   type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#ont)
        
        The event type to add a listen for.
        
    -   listener: (event: [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#ont)\]) \=> void
        
        The function to be called when the event is fired. The listener function is called with the data object passed to `fire`, extended with `target` and `type` properties.
        
    
    #### Returns [Subscription](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Subscription/)
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[on](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#on)
    

### onAdd[](#onadd)

-   onAdd(map: [MapMLGL](https://docs.maptiler.com/sdk-js/api-reference/classes/MapMLGL/)): [HTMLElement](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Externals.HTMLElement/)[](#onadd-1)
    
    Register a control on the map and give it a chance to register event listeners and resources. This method is called by [Map.addControl](https://docs.maptiler.com/sdk-js/api-reference/classes/Map/#addcontrol) internally.
    
    #### Parameters
    
    -   map: [MapMLGL](https://docs.maptiler.com/sdk-js/api-reference/classes/MapMLGL/)
        
        the Map this control will be added to
        
    
    #### Returns [HTMLElement](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Externals.HTMLElement/)
    
    The control's container element. This should be created by the control and returned by onAdd without being attached to the DOM: the map will insert the control's element into the DOM as necessary.
    
    Implementation of [IControl](https://docs.maptiler.com/sdk-js/api-reference/interfaces/IControl/).[onAdd](https://docs.maptiler.com/sdk-js/api-reference/interfaces/IControl/#onadd)
    

### once[](#once)

-   once<[T](#oncet) extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\>(  
        type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet),  
    ): Promise<[GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet)\]\>[](#once-1)
    
    Adds a listener that will be called only once to a specified event type.
    
    The listener will be called first time the event fires after the listener is registered.
    
    #### Type Parameters
    
    -   T extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)
    
    #### Parameters
    
    -   type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet)
        
        The event type to listen for.
        
    
    #### Returns Promise<[GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet)\]\>
    
    a promise that resolves with the event
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[once](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#once)
    
-   once<[T](#oncet-1) extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\>(  
        type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet-1),  
        listener: (event: [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet-1)\]) \=> void,  
    ): this[](#once-2)
    
    Adds a listener that will be called only once to a specified event type.
    
    The listener will be called first time the event fires after the listener is registered.
    
    #### Type Parameters
    
    -   T extends keyof [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)
    
    #### Parameters
    
    -   type: [T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet-1)
        
        The event type to listen for.
        
    -   listener: (event: [GeolocateControlEventType](https://docs.maptiler.com/sdk-js/api-reference/types/GeolocateControlEventType/)\[[T](https://docs.maptiler.com/sdk-js/api-reference/classes/MaptilerGeolocateControl/#oncet-1)\]) \=> void
        
        The function to be called when the event is fired the first time.
        
    
    #### Returns this
    
    `this` when a listener is provided
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[once](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#once)
    

### onRemove[](#onremove)

-   onRemove(): void[](#onremove-1)
    
    Unregister a control on the map and give it a chance to detach event listeners and resources. This method is called by [Map.removeControl](https://docs.maptiler.com/sdk-js/api-reference/classes/Map/#removecontrol) internally.
    
    #### Returns void
    
    Implementation of [IControl](https://docs.maptiler.com/sdk-js/api-reference/interfaces/IControl/).[onRemove](https://docs.maptiler.com/sdk-js/api-reference/interfaces/IControl/#onremove)
    

### setEventedParent[](#seteventedparent)

-   setEventedParent(  
        parent?: [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/)<[EventTypeMap](https://docs.maptiler.com/sdk-js/api-reference/types/EventTypeMap/)\> | null,  
        data?: [EventedParentData](https://docs.maptiler.com/sdk-js/api-reference/types/EventedParentData/) | (() \=> [EventedParentData](https://docs.maptiler.com/sdk-js/api-reference/types/EventedParentData/)),  
    ): this[](#seteventedparent-1)
    
    Bubble all events fired by this instance of Evented to this parent instance of Evented.
    
    #### Parameters
    
    -   `Optional`parent: [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/)<[EventTypeMap](https://docs.maptiler.com/sdk-js/api-reference/types/EventTypeMap/)\> | null
    -   `Optional`data: [EventedParentData](https://docs.maptiler.com/sdk-js/api-reference/types/EventedParentData/) | (() \=> [EventedParentData](https://docs.maptiler.com/sdk-js/api-reference/types/EventedParentData/))
    
    #### Returns this
    
    Inherited from [Evented](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/).[setEventedParent](https://docs.maptiler.com/sdk-js/api-reference/classes/Evented/#seteventedparent)
    

### trigger[](#trigger)

-   trigger(): boolean[](#trigger-1)
    
    Programmatically request and move the map to the user's location.
    
    #### Returns boolean
    
    `false` if called before control was added to a map, otherwise returns `true`.
    
    #### Example[](#example-8)
    
    ```typescript
    // Initialize the geolocate control.
    let geolocate = new GeolocateControl({
     positionOptions: {
       enableHighAccuracy: true
     },
     trackUserLocation: true
    });
    // Add the control to the map.
    map.addControl(geolocate);
    map.on('load', () => {
      geolocate.trigger();
    });
    ```
