wx.getLocation(Object object)
获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。开启高精度定位,接口耗时会增加,可指定 highAccuracyExpireTime 作为超时时间。
wx.getLocation({
type: 'wgs84',//wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success (res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
}
})
执行上述代码,就会出现如下弹框:
wx.getSetting(Object object)
获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
wx.getSetting({
withSubscriptions: true, //是否同时获取用户订阅消息的订阅状态,默认不获取
success (res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.subscribeMessage": true
// }
console.log(res.subscriptionsSetting)
// res.subscriptionsSetting = {
// SYS_MSG_TYPE_INTERACTIVE: 'accept',
// SYS_MSG_TYPE_RANK: 'accept',
// zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject',
// ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban',
// }
}
})
注:如果没有优先执行wx.getLocation(Object object),res.authSetting[‘scope.userLocation’]的结果为undefined。如果获取当前地理位置res.authSetting[‘scope.userLocation’]的结果为true,否则为false
wx.openSetting(Object object)
调起客户端小程序设置界面,返回用户设置的操作结果。设置界面只会出现小程序已经向用户请求过的权限。
注:在执行wx.getLocation(Object object)方法后,没有获取当前地理位置,还需获取地理位置时,需要先调起设置界面,如果没有执行过wx.getLocation(Object object)方法,上图“设置”界面将不会显示“是用我的地理位置”按钮。
总结:
小程序获取当前用户地理位置时,必须先执行一次wx.getLocation(Object object)方法,这样wx.getSetting(Object object)方法获取res.authSetting[‘scope.userLocation’]的结果不是true就是false。如果没有执行过wx.getLocation(Object object)方法,res.authSetting[‘scope.userLocation’]的结果为undefined。