• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

微信小程序获取地理位置授权,首先需要在app.json中添加配置:

"permission": {
    "scope.userLocation": {
      "desc": "请确认授权"
    }
  }

获取经纬度:如果手机未开启位置信息,那么授权成功后在wx.getLocation()方法中也会一直失败,所以需要在fail方法中提示用户开启手机位置信息

getUserLocation: function () {
        let vm = this
        wx.getSetting({
            success: (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) {
                    // console.log(\'authSetting:status:拒绝授权后再次进入重新授权\', res.authSetting[\'scope.userLocation\'])
                    wx.showModal({
                        title: \'\',
                        content: \'【泰福利Lite】需要获取你的地理位置,请确认授权\',
                        success: function (res) {
                            if (res.cancel) {
                                wx.showToast({
                                    title: \'拒绝授权\',
                                    icon: \'none\'
                                })
                                setTimeout(() => {
                                    wx.navigateBack()
                                }, 1500)
                            } else if (res.confirm) {
                                wx.openSetting({
                                    success: function (dataAu) {
                                        // console.log(\'dataAu:success\', dataAu)
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            //再次授权,调用wx.getLocation的API
                                            vm.getLocation(dataAu)
                                        } else {
                                            wx.showToast({
                                                title: \'授权失败\',
                                                icon: \'none\'
                                            })
                                            setTimeout(() => {
                                                wx.navigateBack()
                                            }, 1500)
                                        }
                                    }
                                })
                            }
                        }
                    })
                }
                // 初始化进入,未授权
                else if (res.authSetting[\'scope.userLocation\'] == undefined) {
                    // console.log(\'authSetting:status:初始化进入,未授权\', res.authSetting[\'scope.userLocation\'])
                    //调用wx.getLocation的API
                    vm.getLocation(res)
                }
                // 已授权
                else if (res.authSetting[\'scope.userLocation\']) {
                    // console.log(\'authSetting:status:已授权\', res.authSetting[\'scope.userLocation\'])
                    //调用wx.getLocation的API
                    vm.getLocation(res)
                }
            }
        })
    },
    // 微信获得经纬度
    getLocation: function (userLocation) {
        let vm = this
        wx.getLocation({
            type: "wgs84",
            success: function (res) {
                // console.log(\'getLocation:success\', res)
                var latitude = res.latitude
                var longitude = res.longitude
                vm.getDaiShu(latitude, longitude)
            },
            fail: function (res) {
                // console.log(\'getLocation:fail\', res)
                if (res.errMsg === \'getLocation:fail:auth denied\') {
                    wx.showToast({
                        title: \'拒绝授权\',
                        icon: \'none\'
                    })
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 1500)
                    return
                }
                if (!userLocation || !userLocation.authSetting[\'scope.userLocation\']) {
                    vm.getUserLocation()
                } else if (userLocation.authSetting[\'scope.userLocation\']) {
                    wx.showModal({
                        title: \'\',
                        content: \'请在系统设置中打开定位服务\',
                        showCancel: false,
                        success: result => {
                            if (result.confirm) {
                                wx.navigateBack()
                            }
                        }
                    })
                } else {
                    wx.showToast({
                        title: \'授权失败\',
                        icon: \'none\'
                    })
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 1500)
                }
            }
        })
    }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序-获取当前位置和城市名发布时间:2022-07-18
下一篇:
微信小程序位置发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap