---
title: "MaplibreGeolocateControl"
description: "定位控件，点击后请求浏览器定位并在地图上跟踪用户位置与方向。"
canonical_url: "https://maplibre.mhaibaraai.cn/docs/controls/geolocate"
---
# MaplibreGeolocateControl

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

## 简介

`MaplibreGeolocateControl` 提供定位按钮：点击后请求浏览器地理定位并将视图移至用户位置。`options` 直通 maplibre-gl 的 `GeolocateControl` 构造选项，`trackUserLocation` 持续跟踪、`showAccuracyCircle` 控制精度圈、`positionOptions.enableHighAccuracy` 提升精度。

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

## 用法

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

```vue [GeolocateControlExample.vue]
<template>
  <div class="h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MaplibreMap :options="{ style: 'https://tiles.openfreemap.org/styles/positron', center: [116.39, 39.91], zoom: 10 }">
      <!-- 点击控件触发浏览器定位授权，授权后持续跟踪并显示位置与精度圈 -->
      <MaplibreGeolocateControl
        position="top-right"
        :options="{
          trackUserLocation: true,
          positionOptions: { enableHighAccuracy: true }
        }"
      />
    </MaplibreMap>
  </div>
</template>
```

## API

### Props

```ts
/**
 * Props for the MaplibreGeolocateControl component
 */
interface MaplibreGeolocateControlProps {
  /**
   * 控件停靠位置；省略用地图默认位置
   */
  position?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | undefined;
  /**
   * GeolocateControl 构造选项
   */
  options?: GeolocateControlOptions | undefined;
}
```

## Changelog

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


## Sitemap

See the full [sitemap](https://maplibre.mhaibaraai.cn/sitemap.md) for all pages.
