uniapp-微信小程序获取定位授权

//  获取用户的地理位置,
getLocation() {
    uni.getLocation({
        type: 'gcj02',
        altitude: true,
        success(res) {
            let latAndLon = {
                mylog: res.longitude,
                mylat: res.latitude
            }
            uni.setStorageSync('latAndLon', latAndLon)
        }
    })
},
    /*判断是否已经授权 */
    getSetting() {
        const that = this
        uni.getSetting({
            success(res) {
                if (res.authSetting['scope.userLocation']) {
                    that.getLocation()
                } else {
                    that.getAuthorize()
                }
            }
        })
    },
        // 自动授权
        getAuthorize() {
            let that = this
            uni.authorize({
                scope: 'scope.userLocation',
                success(res) {
                    that.getLocation()
                },
                // 授权失败
                fail(err) {
                    uni.showModal({
                        title: '提示',
                        content: '请授权位置获取附近的商家!',
                        showCancel: false,
                        confirmText: '确认授权',
                        success() {
                            uni.openSetting({
                                success(res) {},
                                fail(err) {}
                            })
                        }
                    })
                }
            })
        },

你可能感兴趣的:(uniapp,个人原创,微信小程序,前端,小程序)