Skip to content

useGeolocation

类别
导出大小
552 B
最近修改
6 months ago

响应式 地理位置 API。它允许用户在愿意的情况下向网页应用提供他们的位置。出于隐私原因,用户需要被请求许可以报告位置信息。

🌐 Reactive Geolocation API. It allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information.

示例

{
  "coords": {
    "accuracy": 0,
    "latitude": null,
    "longitude": null,
    "altitude": null,
    "altitudeAccuracy": null,
    "heading": null,
    "speed": null
  },
  "locatedAt": null,
  "error": null
}

用法

🌐 Usage

ts
import { 
useGeolocation
} from '@vueuse/core'
const {
coords
,
locatedAt
,
error
,
resume
,
pause
} =
useGeolocation
()
状态类型描述
coordsCoordinates获取的位置相关信息,例如纬度和经度
locatedAtDate上一次地理定位调用的时间
errorstring当地理定位 API 失败时的错误信息
resumefunction恢复地理定位更新的控制函数
pausefunction暂停地理定位更新的控制函数

配置

🌐 Config

useGeolocation 函数将 PositionOptions 对象作为可选参数。

组件用法

🌐 Component Usage

vue
<template>
  <UseGeolocation v-slot="{ coords: { 
latitude
,
longitude
} }">
Latitude: {{
latitude
}}
Longitude: {{
longitude
}}
</UseGeolocation> </template>