位置

最近更新时间:2023-10-20 15:19:19

我的收藏

getLocation

该 API 使用方法为 wx.getLocation(Object object)
说明:
调用前需要“用户授权” scope.userLocation。
功能说明:获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。
参数及说明:Object object。
属性
类型
默认值
必填
说明
type
string
wgs84
wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
altitude
string
false
传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数参数:Object res
属性
类型
说明
latitude
number
纬度,范围为 -90~90,负数表示南纬
longitude
number
经度,范围为 -180~180,负数表示西经
speed
number
速度,单位 m/s
accuracy
number
位置的精确度
altitude
number
高度,单位 m
verticalAccuracy
number
垂直精度,单位 m(Android 无法获取,返回 0)
horizontalAccuracy
number
水平精度,单位 m
示例代码:
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
}
})
说明:
工具中定位模拟使用 IP 定位,可能会有一定误差。且工具目前仅支持 gcj02 坐标。
使用第三方服务进行逆地址解析时,请确认第三方服务默认的坐标系,正确进行坐标转换。

choosePoi

该 API 使用方法为 wx.choosePoi(Object object)
功能描述:打开 POI 列表选择位置,支持模糊定位(精确到市)和精确定位混选。
参数及说明:Object object。
属性
类型
默认值
必填
说明
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数参数:Object res。
属性
类型
说明
type
number
选择城市时,值为1,选择精确位置时,值为2
city
number
城市名称
name
string
位置名称
address
string
详细地址
latitude
number
纬度,浮点数,范围为-90~90,负数表示南纬。使用 gcj02 国测局坐标系(即将废弃)
longitude
number
经度,浮点数,范围为-180~180,负数表示西经。使用 gcj02 国测局坐标系(即将废弃)
示例:


chooseLocation

该 API 使用方法为 wx.chooseLocation(Object object)
功能描述:打开地图选择位置。
参数及说明:Object object。
属性
类型
默认值
必填
说明
type
string
tencent
腾讯地图 tencent,谷歌地图 google,仅 IDE 支持
latitude
number
-
目标地纬度
longitude
number
-
目标地经度
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数参数:Object res。
属性
类型
说明
name
string
位置名称
address
string
详细地址
latitude
number
纬度,浮点数,范围为-90~90,负数表示南纬。使用 gcj02 国测局坐标系
longitude
number
经度,浮点数,范围为-180~180,负数表示西经。使用 gcj02 国测局坐标系
示例:


stopLocationUpdate

该 API 使用方法为 wx.stopLocationUpdate(Object object)
功能说明:关闭监听实时位置变化,前后台都停止消息接收。
参数及说明:Object object。
属性
类型
默认值
必填
说明
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)


startLocationUpdateBackground

该 API 使用方法为 wx.startLocationUpdateBackground(Object object)
功能说明:开启小程序进入前后台时均接收位置消息,需引导用户开启'授权'。授权以后,小程序在运行中或进入后台均可接受位置消息变化。
参数及说明:Object object。
属性
类型
默认值
必填
说明
type
string
gcj02
wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)

startLocationUpdate

该 API 使用方法为 wx.startLocationUpdate(Object object)
功能说明:开启小程序进入前台时接收位置消息。
参数及说明:Object object。
属性
类型
默认值
必填
说明
type
string
坐标,gcj02
wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)

openLocation

该 API 使用方法为 wx.openLocation(Object object)
功能说明:使用内置地图查看位置。
参数及说明:Object object。
属性
类型
默认值
必填
说明
type
string
tencent
腾讯地图 tencent,谷歌地图 google,仅 IDE 支持
latitude
number
-
纬度,范围为-90~90,负数表示南纬。使用 gcj02 国测局坐标系
longitude
number
-
经度,范围为-180~180,负数表示西经。使用 gcj02 国测局坐标系
scale
number
18
缩放比例,范围5~18
name
string
-
位置名
address
string
-
地址的详细说明
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
示例代码
wx.getLocation({
type: 'gcj02', //返回可以用于 wx.openLocation 的经纬度
success (res) {
const latitude = res.latitude
const longitude = res.longitude
wx.openLocation({
latitude,
longitude,
scale: 18
})
}
})

onLocationChangeError

该 API 使用方法为 wx.onLocationChangeError(function listener)
功能说明:取消监听持续定位接口返回失败时触发。
参数及说明:function callback 的回调函数。

onLocationChange

该 API 使用方法为 wx.onLocationChange(function listener)
功能说明:监听实时地理位置变化事件,需结合 wx.startLocationUpdateBackgroundwx.startLocationUpdate使用。
参数及说明:function callback。
实时地理位置变化事件的回调函数参数:Object res。
属性
类型
说明
latitude
number
纬度,范围为 -90~90,负数表示南纬。使用 gcj02 国测局坐标系
longitude
number
经度,范围为 -180~180,负数表示西经。使用 gcj02 国测局坐标系
speed
number
速度,单位 m/s
accuracy
number
位置的精确度
altitude
number
高度,单位m
verticalAccuracy
number
垂直精度,单位m(Android 无法获取,返回0)
horizontalAccuracy
number
水平精度,单位m
示例代码
const _locationChangeFn = function(res) {
console.log('location change', res)
}
wx.onLocationChange(_locationChangeFn)
wx.offLocationChange(_locationChangeFn)

offLocationChangeError

该 API 使用方法为 wx.offLocationChangeError(function listener)
功能说明:取消监听持续定位接口返回失败时触发。
参数及说明:function callback 的回调函数。

offLocationChange

该 API 使用方法为 wx.offLocationChange(function listener)
功能说明:取消监听实时地理位置变化事件。
参数及说明:function callback 实时地理位置变化事件的回调函数。

getFuzzyLocation

该 API 使用方法为 wx.getFuzzyLocation(Object object)
功能说明:获取当前的模糊地理位置。
参数及说明:Object object。
属性
类型
默认值
必填
说明
type
string
wgs84
wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数参数:Object res。
属性
类型
说明
latitude
number
纬度,范围为 -90~90,负数表示南纬
longitude
number
经度,范围为 -180~180,负数表示西经
示例代码
wx.getFuzzyLocation({
type: 'wgs84',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
}
})