微信小程序?qū)τ诘刂返娜N處理。
-
wx.openLocation:展示所處地址 -
wx.getLocation():獲取當(dāng)前地址 -
wx.chooseLocation():打開地圖選擇地址
wx.openLocation:使用微信內(nèi)置地圖查看位置。
- 不需要授權(quán)。
- 可以跳轉(zhuǎn)導(dǎo)航APP進(jìn)行導(dǎo)航。
wx.openLocation({
latitude: this.latitude,
longitude: this.longitude,
name: this.name, // 名稱
address: this.address, // 地址
scale: 28
});

wx.getLocation() :獲取當(dāng)前的地理位置、速度。
- 調(diào)用前需要用戶授權(quán)
scope.userLocation。 - type 默認(rèn) wgs84 坐標(biāo),返回 gps 坐標(biāo), type 為 gcj02 返回可用于
wx.openLocation的坐標(biāo)。
wx.getLocation({
type: 'gcj02', // 返回可以用于wx.openLocation的經(jīng)緯度
success(res) {
const latitude = res.latitude
const longitude = res.longitude
wx.openLocation({
latitude,
longitude,
scale: 18
});
}
});

最近這個接口有了新的要求,要先在 app 配置說明。
微信客戶端 7.0.0 及以上版本支持

desc 會在彈窗的標(biāo)題下作為說明。

但有時候用戶拒絕了授權(quán),這樣的話,你再次進(jìn)入的話就不會彈出彈窗了,如果你還想再次請求授權(quán)的話,就要用到 wx.getSetting。
wx.getSetting:獲取用戶的當(dāng)前設(shè)置。返回值中只會出現(xiàn)小程序已經(jīng)向用戶請求過的權(quán)限。
要注意是,請求過的權(quán)限,不是用戶所有的權(quán)限。
wx.getSetting({
success(res) {
console.log(res.authSetting);
// 具體API的授權(quán)字段見權(quán)限頁面
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
}
})
既然用 wx.getSetting 獲取到用戶已經(jīng)授權(quán)過的權(quán)限。如果用戶拒絕授權(quán),"scope.userInfo": false,若是 "scope.userInfo": true,則用戶授權(quán)成功。
而用戶沒有申請過位置的授權(quán)的話,則scope.userInfo 不會出現(xiàn)在 res 里。
如果用戶拒絕授權(quán)后,要想再次打開授權(quán)窗口,就要用到wx.openSetting。
wx.openSetting:調(diào)起客戶端小程序設(shè)置界面,返回用戶設(shè)置的操作結(jié)果。設(shè)置界面只會出現(xiàn)小程序已經(jīng)向用戶請求過的權(quán)限。
在用戶沒有申請過授權(quán)的情況下,不能調(diào)用此接口,因?yàn)槭跈?quán)頁面不會出現(xiàn)你想要但用戶沒有申請過的授權(quán)接口。

以下是申請定位功能的代碼:
wx.getSetting({
success(res) {
console.log('get-setting', res.authSetting);
// 只返回用戶請求過的授權(quán)
let auth = res.authSetting;
if (auth['scope.userLocation']) {
// 已授權(quán),申請定位地址
that.getUserLocation();
} else if (auth['scope.userLocation'] === undefined) {
// 用戶沒有請求過的授權(quán),不需要我們主動彈窗,微信會提供彈窗
that.getUserLocation();
} else if (!auth['scope.userLocation']) {
// 沒有授權(quán)過,需要用戶重新授權(quán)
// 這個彈窗是為了實(shí)現(xiàn)點(diǎn)擊,不然openSetting會失敗
wx.showModal({
title: '是否授權(quán)當(dāng)前位置?',
content: '需要獲取您的地理位置,請確認(rèn)授權(quán),否則定位功能將無法使用',
success: res => {
if (res.confirm) {
wx.openSetting({
success(res) {
console.log('open-setting-suc', res.authSetting);
let setting = res.authSetting;
if (!setting['scope.userLocation']) {
wx.showToast({
title: '地址授權(quán)失敗,定位功能無法使用',
icon: 'none',
});
} else {
// 地址授權(quán)成功,申請定位地址
that.getUserLocation();
}
},
fail(err) {
// 需要點(diǎn)擊,有時候沒有點(diǎn)擊,是無法觸發(fā)openSetting
console.log('open-setting-fail',err);
}
});
}
}
});
}
}
});
async getUserLocation () {
let res = await wepy.getLocation();
this.longitude = res.longitude;
this.latitude = res.latitude;
this.$apply();
}
wx.chooseLocation:打開地圖選擇位置。
- 調(diào)用前需要用戶授權(quán) scope.userLocation。
- 和
wx.getLocation()的權(quán)限字段一樣,都是scope.userLocation。 - 返回位置名稱,詳細(xì)地址,gcj02 坐標(biāo)。
- 和
wx.getLocation()的代碼大同小異
wx.chooseLocation需要用戶授權(quán)成功后才能調(diào)用成功。

具體授權(quán)表:

授權(quán)具體做法:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html