小程序wx.chooseLocation地圖選點確認地址


1、要實現這個功能首先分析地圖選點需要的條件

  • 用戶手機開啟微信定位權限(針對iOS和Android做兼容)
  • 用戶允許定位授權
  • 微信API-wx.chooseLocation(Object Object)

2、條件分析完,下面開始代碼

  iOS和Android微信定位權限的兼容:

  wx.chooseLocation在安卓中的fail回調沒有直接的表示微信不允許定位權限,在wx.getLocation的fail回調中通過判斷errMsg可以判斷用戶是否開啟定位權限

  代碼如下:

let systemInfo = wx.getSystemInfoSync();
_this.setData({
  systemInfo,
})
if(_this.data.systemInfo.system.indexOf('Android') >= 0){
        //安卓系統
        wx.getLocation({
            success:function (res) {
                console.log('getLocation res success:', res);
                chooseLocation.call(_this)
            },
            fail:function (res) {
                console.log('getLocation res fail:', res);
                // 系統關閉定位
                if(res.errMsg === 'getLocation:fail:system permission denied' || res.errMsg === 'getLocation:fail:system permission deny'){
                    return wx.showModal({
                        title:'無法獲取你的位置信息',
                        content:'請到手機系統的[設置]->[位置信息]中打開定位服務,並允許微信使用定位服務。',
                        showCancel:false,
                        confirmText:'確定',
                        confirmColor:'#0052A4'
                    })
                }
                //用戶取消授權
                if(res.errMsg === 'getLocation:fail:auth deny' || res.errMsg === 'getLocation:fail:auth denied'){
                    //用戶取消授權
                    _this.setData({cancelLoaction:true})
                    getSetting.call(_this)
                }
            }
        })
    }else{
        chooseLocation.call(_this)
    }

  小程序在調用微信位置的時候需要用戶授權允許,API提供了wx.getSetting

  代碼如下:

const getSetting = function () {
    let _this = this;
    wx.getSetting({
        success:function (res) {
            let authSetting = res.authSetting;
            console.log('getSetting',authSetting);
            //是否授權地理位置
            console.log(authSetting['scope.userLocation'], '2222222222222');
            if(authSetting['scope.userLocation']){
                getLocation.call(_this)
            }else{
                wx.openSetting({
                    success:function (res) {
                        let authSetting = res.authSetting;
                        console.log('open setting', authSetting);
                        if(authSetting['scope.userLocation']){
                            _this.setData({cancelLoaction:false})
                            getLocation.call(_this)
                        }else{

                        }
                    }
                })
            }
        }
    })
}

  cancelLoaction,對於用戶有可能誤點了“不允許”,字段對這個做了判斷

  下面就是事件觸發

setPoidAdd: function () {
        let _this = this;
        if(_this.data.cancelLoaction){
            getSetting.call(_this)
        }else{
            getLocation.call(_this)
        }
    },

  最重要的是還是wx.chooseLocation

const chooseLocation = function () {
        wx.chooseLocation({
            success:function (res) {
                console.log('chooseLocation',res);
                if(res.address !== '' && res.name !== ''){
            //確認選擇地址之后adress和name才不會為空
let maddr
= _this.data.maddr; maddr.poiaddress = res.address + res.name; maddr.longitude = res.longitude; maddr.latitude = res.latitude; _this.setData({ maddr, }) } }, fail:function (res) { console.log('fail', res); if(res.errMsg === 'chooseLocation:fail cancel') return; //res.errMsg === 'chooseLocation:fail auth deny' || res.errMsg === 'chooseLocation:fail auth denied' //用戶取消授權 if(res.errMsg){ _this.setData({cancelLoaction:true}) getSetting.call(_this) } } }) }

  PS:機型測試這邊發現華為P20是不ok的,報的錯誤時服務器錯誤,並且不止是這個用例測試失敗,其他wxApp也是不OK的,考慮可能是因為手機定位服務問題,其他暫未發現什么問題。

  附上getLocation方法完整代碼

const getLocation = function () {
    let _this = this;
    const chooseLocation = function () {
        wx.chooseLocation({
            success:function (res) {
                console.log('chooseLocation',res);
                if(res.address !== '' && res.name !== ''){
                    //確認選擇地址之后adress和name才不會為空
                    let maddr = _this.data.maddr;
                    maddr.poiaddress = res.address + res.name;
                    maddr.longitude = res.longitude;
                    maddr.latitude = res.latitude;
                    _this.setData({
                        maddr,
                    })
                }
            },
            fail:function (res) {
                console.log('fail', res);
                if(res.errMsg === 'chooseLocation:fail cancel') return;
                //res.errMsg === 'chooseLocation:fail auth deny' || res.errMsg === 'chooseLocation:fail auth denied'
                //用戶取消授權
                if(res.errMsg){
                    _this.setData({cancelLoaction:true})
                    getSetting.call(_this)
                }
            }
        })
    }
    console.log('systemInfo res:', _this.data.systemInfo);
    if(_this.data.systemInfo.system.indexOf('Android') >= 0){
        //安卓系統
        wx.getLocation({
            success:function (res) {
                console.log('getLocation res success:', res);
                chooseLocation.call(_this)
            },
            fail:function (res) {
                console.log('getLocation res fail:', res);
                // 系統關閉定位
                if(res.errMsg === 'getLocation:fail:system permission denied' || res.errMsg === 'getLocation:fail:system permission deny'){
                    return wx.showModal({
                        title:'無法獲取你的位置信息',
                        content:'請到手機系統的[設置]->[位置信息]中打開定位服務,並允許微信使用定位服務。',
                        showCancel:false,
                        confirmText:'確定',
                        confirmColor:'#0052A4'
                    })
                }
                //用戶取消授權
                if(res.errMsg === 'getLocation:fail:auth deny' || res.errMsg === 'getLocation:fail:auth denied'){
                    //用戶取消授權
                    _this.setData({cancelLoaction:true})
                    getSetting.call(_this)
                }
            }
        })
    }else{
        chooseLocation.call(_this)
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM