微信小程序判断用户是否授权定位

当授权地理位置时候用户点击了拒绝之后的流程图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

点击允许授权则获取当前定位信息进行一些业务操作

//在uniapp/小程序全局文件中app.vue/main.js
onLoad(){
    
    
	this.checkLocationAuth();
}
getLocation() {
    
    
		  return new Promise((resolve, reject) => {
    
    
			  let that =this
					wx.getLocation({
    
    
					  type: "gcj02",
					  isHighAccuracy: "true",
					  success: (res) => {
    
    
					    console.log(res)
					    console.log(res.longitude, 'getLocation获取当前经纬度')
					    console.log(res.latitude, 'getLocation获取当前经纬度')
					    uni.request({
    
    
					      url: this.globalData.URL + "subway/getSubway",
					      method: 'POST',
					      data: {
    
    
					        version: '251',
					        client: 'wxmp',
					        latitude: res.latitude + 0.001276,
					        longitude: res.longitude + 0.006256
					      },
					      success: (res) => {
    
    
					        console.log(res, '地铁站计算距离')
					        this.globalData.fujin_sub = res.data.data.result.geo_subway
					        this.globalData.jwd = res.data.data.result.location
					        uni.setStorageSync('getCity', res.data.data.result.addressComponent);
					        if (res.data.data.result.addressComponent.cityId == '0') {
    
    
					          uni.setStorageSync('localcityId', 9999);
					        } else {
    
    
					          uni.setStorageSync('localcityId', res.data.data.result.addressComponent.cityId);
					        }
					        uni.setStorageSync('localcityNm', res.data.data.result.geo_subway);
					        uni.setStorageSync('City', res.data.data.result.location);
					        resolve(res.data.data.result.location)
					      }
					    })
					  },
					  fail: () => {
    
    
					    reject('getLocation failed')
					  }
					});
		  })
		},
		//单独提取一个判断用户是否授权定位的函数,在需要的地方直接调用,避免了重复触发getLocation获取定位弹窗	 
		checkLocationAuth() {
    
    
			   wx.getSetting({
    
    
			     success: (res) => {
    
    
			       let authSetting = res.authSetting
			       if (authSetting['scope.userLocation']) {
    
    
			         // 已授权
			         this.getLocation()
			       } else if (authSetting['scope.userLocation'] === false) {
    
    
			         wx.showModal({
    
    
			           title: '您未开启地理位置授权',
			           content: '请在系统设置中打开位置授权,以便我们为您提供更好的服务',
			           success: (res) => {
    
    
			             if (res.confirm) {
    
    
			               wx.openSetting()
			             }
			           }
			         })
			       } else {
    
    
			         wx.authorize({
    
    
			           scope: 'scope.userLocation',
			           success: () => {
    
    
			             this.getLocation()
			           },
			           fail: () => {
    
    
			             wx.showModal({
    
    
			               title: '您未开启地理位置授权',
			               content: '请在系统设置中打开位置授权,以便我们为您提供更好的服务',
			               success: (res) => {
    
    
			                 if (res.confirm) {
    
    
			                   wx.openSetting()
			                 }
			               }
			             })
			           }
			         })
			       }
			     }
			   })
			 },

猜你喜欢

转载自blog.csdn.net/qq_47272950/article/details/130762181
今日推荐