---
title: "<ScriptMapLibreGeolocateControl> · Nuxt Scripts"
description: "Adds MapLibre's geolocate button to the nearest parent map. The button asks the browser for the user's location and moves the camera there."
canonical_url: "https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control"
meta:
  "og:description": "Adds MapLibre's geolocate button to the nearest parent map. The button asks the browser for the user's location and moves the camera there."
  "og:image": "https://scripts.nuxt.com/_og/s/o_6a11e225693ff033.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "<ScriptMapLibreGeolocateControl> · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/s/o_6a11e225693ff033.png"
---

> Index of every page on this site: [llms.txt](https://scripts.nuxt.com/llms.txt)

**Api**

# **\<ScriptMapLibreGeolocateControl>**

[Copy for LLMs](https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control.md)

Adds MapLibre's geolocate button to the nearest parent map. The button asks the browser for the user's location and moves the camera there.

```vue
<script setup lang="ts">
import type { ScriptMapLibreGeolocateControlEmits } from '@nuxt/scripts'

const message = ref('')

function onError(event: ScriptMapLibreGeolocateControlEmits['error'][0]) {
  message.value = event.code === 1 ? 'Location permission denied.' : event.message
}
</script>

<template>
  <ScriptMapLibreMap map-style="https://tiles.openfreemap.org/styles/liberty" :center="[147.33, -42.88]">
    <ScriptMapLibreGeolocateControl
      position="top-left"
      :options="{ trackUserLocation: true }"
      @error="onError"
      @unavailable="message = 'Location is not available in this browser.'"
    />
  </ScriptMapLibreMap>
  <p role="status">
    {{ message }}
  </p>
</template>
```

`**new maplibregl.GeolocateControl()**` reads `**options**` and `**position**` once, when it creates the control. To apply a change, change the component `**key**`.

The component exposes the MapLibre control as `**control**`. Call `**control.value.trigger()**` to request the location from code.

## Events

The component emits every `**GeolocateControl**` event with MapLibre's event object:

- `**geolocate**` fires when the browser returns a position.
- `**error**` fires when the browser returns an error.
- `**outofmaxbounds**` fires when the position is outside the map's `**maxBounds**`.
- `**trackuserlocationstart**` and `**trackuserlocationend**` fire when tracking starts and stops. They need `**trackUserLocation**`.
- `**userlocationfocus**` and `**userlocationlostfocus**` fire when the camera locks to or leaves the user's location.

## Permission denied

Geolocation can fail in two ways. The component reports both.

- If the user denies the permission prompt, `**error**` fires with `**code**` 1. MapLibre then disables the button.
- If the permission was already denied when the control loaded, MapLibre disables the button and fires no event. The component emits `**unavailable**` with `**'permission-denied'**`.
- If the browser has no Geolocation API, the component emits `**unavailable**` with `**'unsupported'**`.

Geolocation needs a secure context. On a plain HTTP origin other than `**localhost**`, the browser denies it.

`**position**``MapLibre.ControlPosition`

`**options**``MapLibre.GeolocateControlOptions`

[Edit this page](https://github.com/nuxt/scripts/edit/main/docs/content/maplibre/2.api/8.geolocate-control.md)

[Markdown For LLMs](https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control.md)

[**\<ScriptMapLibreScaleControl>** Adds MapLibre's scale bar to the nearest parent map. MapLibre places it in the bottom-left corner by default.](https://scripts.nuxt.com/scripts/maplibre/api/scale-control) [**\<ScriptMapLibreFullscreenControl>** Adds MapLibre's fullscreen button to the nearest parent map. The map container goes fullscreen by default. Pass options.container to make a different element fullscreen.](https://scripts.nuxt.com/scripts/maplibre/api/fullscreen-control)

**On this page **

- [Events](#events)
- [Permission denied](#permission-denied)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://scripts.nuxt.com/#website","@type":"WebSite","description":"Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX. It includes many popular third-parties out of the box.","name":"Nuxt Scripts","url":"https://scripts.nuxt.com/","publisher":{"@id":"https://scripts.nuxt.com/#identity"}},{"@id":"https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control#webpage","@type":"WebPage","description":"Adds MapLibre's geolocate button to the nearest parent map. The button asks the browser for the user's location and moves the camera there.","name":"<ScriptMapLibreGeolocateControl>","url":"https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control","breadcrumb":{"@id":"https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control"]}],"primaryImageOfPage":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/#identity","@type":"Organization","name":"Nuxt Scripts","url":"https://scripts.nuxt.com","logo":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Scripts","item":"https://scripts.nuxt.com/scripts","position":2},{"@type":"ListItem","name":"Maplibre","item":"https://scripts.nuxt.com/scripts/maplibre","position":3},{"@type":"ListItem","name":"API","item":"https://scripts.nuxt.com/scripts/maplibre/api","position":4},{"@type":"ListItem","name":"<ScriptMapLibreGeolocateControl>","item":"https://scripts.nuxt.com/scripts/maplibre/api/geolocate-control","position":5}]},{"@id":"https://scripts.nuxt.com/#logo","@type":"ImageObject","caption":"Nuxt Scripts","contentUrl":"https://scripts.nuxt.com/logo.svg","inLanguage":"en","url":"https://scripts.nuxt.com/logo.svg"}]}
```

## Sitemap

See the full [sitemap](https://scripts.nuxt.com/sitemap.md) for all pages.
