码迷,mamicode.com
首页 > 微信 > 详细

微信小程序获取地理位置授权

时间:2019-09-17 19:24:42      阅读:722      评论:0      收藏:0      [点我收藏+]

标签:als   rmi   can   gets   fail   desc   string   color   fine   

getUserLocation: function () {
        let vm = this
        wx.getSetting({
            success: (res) => {
                console.log(‘getUserLocation‘, res)
                // res.authSetting[‘scope.userLocation‘] == undefined    表示 初始化进入该页面
                // res.authSetting[‘scope.userLocation‘] == false    表示 非初始化进入该页面,且未授权
                // res.authSetting[‘scope.userLocation‘] == true    表示 地理位置授权
                if (res.authSetting[‘scope.userLocation‘] != undefined && res.authSetting[‘scope.userLocation‘] != true) {
                    wx.showModal({
                        title: ‘请求授权当前位置‘,
                        content: ‘需要获取您的地理位置,请确认授权‘,
                        success: function (res) {
                            if (res.cancel) {
                                wx.showToast({
                                    title: ‘拒绝授权‘,
                                    icon: ‘none‘,
                                    duration: 1000
                                })
                            } else if (res.confirm) {
                                wx.openSetting({
                                    success: function (dataAu) {
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            wx.showToast({
                                                title: ‘授权成功‘,
                                                icon: ‘success‘,
                                                duration: 1000
                                            })
                                            //再次授权,调用wx.getLocation的API
                                            vm.getLocation()
                                        } else {
                                            wx.showToast({
                                                title: ‘授权失败‘,
                                                icon: ‘none‘,
                                                duration: 1000
                                            })
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else if (res.authSetting[‘scope.userLocation‘] == undefined) {
                    //调用wx.getLocation的API
                    vm.getLocation()
                }
                else {
                    //调用wx.getLocation的API
                    vm.getLocation()
                }
            }
        })
    },
    // 微信获得经纬度
    getLocation: function () {
        let vm = this
        wx.getLocation({
            type: ‘wgs84‘,
            success: function (res) {
                console.log(‘getLocation‘, res)
                var latitude = res.latitude
                var longitude = res.longitude
                vm.getLocal(latitude, longitude)
            },
            fail: function (res) {
                console.log(‘fail‘ + JSON.stringify(res))
            }
        })
    },
    // 获取当前地理位置
    getLocal: function (latitude, longitude) {
        wx.chooseLocation({
            success: function (res) {
                console.log(‘getLocal‘, res)
            }
        })
    },
onLoad() {
  this. getUserLocation()  
}
// app.json中添加permission
"permission": {
    "scope.userLocation": {
      "desc": "小程序需要获取您的授权,请确认"
    }
  },

 

微信小程序获取地理位置授权

标签:als   rmi   can   gets   fail   desc   string   color   fine   

原文地址:https://www.cnblogs.com/memphis-f/p/11535970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!