---
title: "MapboxGeolocateControl"
description: "定位控件，点击后请求浏览器定位并在地图上跟踪用户位置与方向。"
seo_title: "MapboxGeolocateControl Component"
seo_description: "MapboxGeolocateControl requests browser geolocation and tracks the user position and heading on the map, forwarding options to the underlying GeolocateControl."
canonical_url: "https://mapbox.mhaibaraai.cn/docs/controls/geolocate"
---
# MapboxGeolocateControl

> 定位控件，点击后请求浏览器定位并在地图上跟踪用户位置与方向。

## 简介

`MapboxGeolocateControl` 提供定位按钮：点击后请求浏览器地理定位并将视图移至用户位置。`options` 直通 mapbox-gl 的 `GeolocateControl` 构造选项，`trackUserLocation` 持续跟踪、`showUserHeading` 显示方向锥、`positionOptions.enableHighAccuracy` 提升精度。

> \[\!NOTE\]
> 
> 浏览器定位需在 HTTPS（或 localhost）下运行，并由用户在弹窗中授权；未授权或不支持时按钮不会移动视图。

## 用法

点击控件触发定位授权，授权后持续跟踪并显示方向：

```vue [GeolocateControlExample.vue]
<template>
  <div class="h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MapboxMap :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [116.39, 39.91], zoom: 10 }">
      <!-- 点击控件触发浏览器定位授权，授权后持续跟踪并显示方向锥 -->
      <MapboxGeolocateControl
        position="top-right"
        :options="{
          trackUserLocation: true,
          showUserHeading: true,
          positionOptions: { enableHighAccuracy: true }
        }"
      />
    </MapboxMap>
  </div>
</template>
```

## API

### Props

```ts
/**
 * Props for the MapboxGeolocateControl component
 */
interface MapboxGeolocateControlProps {
  /**
   * 控件停靠位置；省略用地图默认位置
   */
  position?: mapboxgl.ControlPosition | undefined;
  /**
   * GeolocateControl 构造选项
   */
  options?: mapboxgl.GeolocateControlOptions | undefined;
}
```

## Changelog

See commit history for [src/runtime/components/controls/GeolocateControl.vue](https://github.com/mhaibaraai/movk-mapbox/commits/main/src/runtime/components/controls/GeolocateControl.vue).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
