小程序:展示,獲取,選擇地址

微信小程序?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
});
查看位置.png

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
        });
      }
    });
第一次請求位置時會出現(xiàn)的彈窗.png

最近這個接口有了新的要求,要先在 app 配置說明。

微信客戶端 7.0.0 及以上版本支持

app的配置

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


desc出現(xiàn)在以往的標(biāo)題下作為說明.png

詳細(xì)公告:https://developers.weixin.qq.com/community/develop/doc/000ea276b44928f7e8d73d0a65b801?idescene=7&op=1

但有時候用戶拒絕了授權(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)接口。


用戶申請過的接口列表.png

以下是申請定位功能的代碼:

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)用成功。

以用戶當(dāng)前位置為中心的地圖,也可以搜索具體地點(diǎn)

具體授權(quán)表:


在wx.getSetting的返回可見請求過的權(quán)限.png

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容