Skip to content

x-location-s 定位

一次性定位、持续定位,以及 WGS84 / GCJ02 / BD09 坐标互转。

兼容性

HarmonyIOSAndroidWEB小程序
支持支持支持支持支持

调用

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)
  }
})

方法

名称需要说明
getLocationtype?altitude?isHighAccuracy?highAccuracyExpireTime?success一次性定位
startLocationUpdatebackground?minUpdateInterval?minUpdateDistance?type?开始持续定位,结果走 onLocationChange
stopLocationUpdate停止持续定位
onLocationChange(res) => void订阅位置变化,返回监听 id
offLocationChangeid取消对应订阅
convertCoordinatefromtolatitudelongitudesuccessWGS84 / GCJ02 / BD09 互转

参数

getLocation

字段说明默认
typewgs84 / gcj02 / bd09wgs84
altitude是否要海拔false
isHighAccuracy高精度false
highAccuracyExpireTime高精度超时 ms8000
success / fail / complete回调-

startLocationUpdate

字段说明默认
background后台持续定位。Web / 微信忽略false
minUpdateInterval最小间隔 ms5000
minUpdateDistance最小距离米,0 不限0
type坐标系wgs84
altitude是否要海拔false
isHighAccuracy高精度false
notificationTitle / notificationTextAndroid 前台服务通知-

XLocationResultlatitudelongitudeaccuracyaltitudespeeddirectioncoordSystemtimestamp

平台差异

  1. Web 需 HTTPS;background: true 会失败。
  2. Android 后台定位走前台服务;Android 11+ 后台权限需用户到设置里开。
  3. iOS 后台需「始终允许」。
  4. 微信须在 app.json 声明定位 scope 与 requiredPrivateInfos
  5. 鸿蒙后台需配置 backgroundModes(可参考插件 harmony-configs)。

版本

版权归https://xui.tmui.design你不得修改及二次开发,仅供TMUI4会员商用使用。不得转给非VIP会员使用,一经查实数倍赔偿,并追究法律责任。

更新日志

x-location-s

1.0.0(2026-07-31)

  • 首次发布
  • 提供 getLocation 获取当前位置
  • 提供 startLocationUpdate / stopLocationUpdate 前后台持续定位
  • 提供 onLocationChange / offLocationChange 位置变化监听
  • 提供 convertCoordinate WGS84 / GCJ02 / BD09 坐标互转
  • 兼容 Android / iOS / Web / 微信小程序 / 鸿蒙
最近更新