x-location-s 定位
一次性定位、持续定位,以及 WGS84 / GCJ02 / BD09 坐标互转。
兼容性
| Harmony | IOS | Android | WEB | 小程序 |
|---|---|---|---|---|
| 支持 | 支持 | 支持 | 支持 | 支持 |
调用
ts
import { getLocation, startLocationUpdate, stopLocationUpdate, onLocationChange, offLocationChange, convertCoordinate } from "@/uni_modules/x-location-s"
getLocation({
type: "wgs84",
isHighAccuracy: true,
success: (res) => {
console.log(res.latitude, res.longitude, res.accuracy)
}
})
const id = onLocationChange((res) => {
console.log(res.latitude, res.longitude)
})
startLocationUpdate({
background: true,
type: "wgs84",
minUpdateInterval: 5000
})
convertCoordinate({
from: "wgs84",
to: "gcj02",
latitude: 39.9,
longitude: 116.4,
success: (res) => {
console.log(res.latitude, res.longitude)
}
})方法
| 名称 | 需要 | 说明 |
|---|---|---|
| getLocation | type?、altitude?、isHighAccuracy?、highAccuracyExpireTime?、success | 一次性定位 |
| startLocationUpdate | background?、minUpdateInterval?、minUpdateDistance?、type? 等 | 开始持续定位,结果走 onLocationChange |
| stopLocationUpdate | 无 | 停止持续定位 |
| onLocationChange | (res) => void | 订阅位置变化,返回监听 id |
| offLocationChange | id | 取消对应订阅 |
| convertCoordinate | from、to、latitude、longitude、success | WGS84 / GCJ02 / BD09 互转 |
参数
getLocation
| 字段 | 说明 | 默认 |
|---|---|---|
| type | wgs84 / gcj02 / bd09 | wgs84 |
| altitude | 是否要海拔 | false |
| isHighAccuracy | 高精度 | false |
| highAccuracyExpireTime | 高精度超时 ms | 8000 |
| success / fail / complete | 回调 | - |
startLocationUpdate
| 字段 | 说明 | 默认 |
|---|---|---|
| background | 后台持续定位。Web / 微信忽略 | false |
| minUpdateInterval | 最小间隔 ms | 5000 |
| minUpdateDistance | 最小距离米,0 不限 | 0 |
| type | 坐标系 | wgs84 |
| altitude | 是否要海拔 | false |
| isHighAccuracy | 高精度 | false |
| notificationTitle / notificationText | Android 前台服务通知 | - |
XLocationResult:latitude、longitude、accuracy、altitude、speed、direction、coordSystem、timestamp。
平台差异
- Web 需 HTTPS;
background: true会失败。 - Android 后台定位走前台服务;Android 11+ 后台权限需用户到设置里开。
- iOS 后台需「始终允许」。
- 微信须在 app.json 声明定位 scope 与
requiredPrivateInfos。 - 鸿蒙后台需配置
backgroundModes(可参考插件harmony-configs)。
版本
版权归https://xui.tmui.design你不得修改及二次开发,仅供TMUI4会员商用使用。不得转给非VIP会员使用,一经查实数倍赔偿,并追究法律责任。
更新日志
x-location-s
1.0.0(2026-07-31)
- 首次发布
- 提供
getLocation获取当前位置 - 提供
startLocationUpdate/stopLocationUpdate前后台持续定位 - 提供
onLocationChange/offLocationChange位置变化监听 - 提供
convertCoordinateWGS84 / GCJ02 / BD09 坐标互转 - 兼容 Android / iOS / Web / 微信小程序 / 鸿蒙
