uniapp 小程序获取地理位置逻辑代码
// todo 获取当前位置经纬度
handleLoacation(toast) {
uni.getLocation({
type: 'gcj02',
success: res => {
console.log('success')
// 获取到经纬度后根据实际业务做处理,下面业务逻辑仅供参考
// 1、先把经纬度存入缓存中
// this.setLocation(res)
// 2、获取到经纬度后再请求对应数据
// this.getData()
},
fail: err => {
// 判断是否获取到了定位
// this.isLocation = false
// 根据不同需求做了两种类型提示,需要根据实际业务修改
if (toast) {
// this.$u.toast是用的框架里封装的弹出消息
let msg = '您已拒绝授权,相关功能会无法使用!'
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'), msg)
this.$u.toast(msg)
return
}
//用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
uni.showModal({
title: '提示',
content: '请在系统设置中打开定位服务,重新进入小程序!'
})
}
})
},
// todo 定位获取位置信息授权逻辑
getLocation() {
let that = this
// wx.getSetting是获取用户授权的信息的,除了应用在位置信息授权还能应用在用户信息授权等等
uni.getSetting({
success: res => {
console.log('获取用户的当前授权设置', res.authSetting)
// 微信返回 {scope.userLocation: false, scope.address: true, scope.invoice: true, scope.invoiceTitle: true, scope.userInfo: true}
// 支付宝返回 {location: false}
// 用户没有打开过小程序的位置授权设置时,支付宝没有location属性,微信没有scope.userLocation属性
// true说明已经授权,如果还拿不到定位信息,说明用户的手机没开启定位功能
let location = undefined
// #ifdef MP-WEIXIN
if (res.authSetting.hasOwnProperty('scope.userLocation')) location = res.authSetting['scope.userLocation']
// #endif
// #ifdef MP-ALIPAY
if (res.authSetting.hasOwnProperty('location')) location = res.authSetting['location']
// #endif
if (location === true) {
console.log('已经授权,非第一次')
//授权后默认加载,直接获取定位
that.handleLoacation()
}
// location === undefined 代表用户未授权且第一次登陆
else if (location === undefined) {
// 如果用户是第一次登陆且未授权的情况,会直接弹窗请求授权
// 使用 getlocation 获取用户 经纬度位置
console.log('第一次登陆且未授权')
that.handleLoacation(true)
}
// 小程序检测到用户不是第一次进入该页面,且未授权
else if (location !== undefined && location !== true) {
console.log('不是第一次进入该页面,且未授权')
that.handleLocationPermission()
}
},
fail: err => {
let msg = '未知错误,请重新打开小程序!'
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'), msg)
uni.showModal({
title: '提示',
content: msg
})
}
})
},
// todo 请求用户授权地理位置信息
handleLocationPermission() {
let that = this
uni.showModal({
title: '是否授权当前位置',
content: '需要获取您的地理位置,请确认授权,否则无法获取门店数据',
success: res => {
// 如果点击取消则显示授权失败
if (res.cancel) {
let msg = '您已拒绝授权!'
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'), msg)
this.$u.toast(msg)
}
// 如果点击确定会打开授权页请求二次授权
else if (res.confirm) {
uni.openSetting({
success: data => {
console.log('调起客户端小程序设置界面,返回用户设置的操作结果', data.authSetting)
// 微信返回 {scope.userLocation: false}
// 支付宝返回 {location: false, userInfo: false}
let location = undefined
// #ifdef MP-WEIXIN
if (data.authSetting.hasOwnProperty('scope.userLocation')) location = data.authSetting['scope.userLocation']
// #endif
// #ifdef MP-ALIPAY
if (data.authSetting.hasOwnProperty('location')) location = data.authSetting['location']
// #endif
if (location === true) {
let msg = '授权成功'
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'), msg)
uni.showToast({
title: msg,
icon: 'success',
duration: 2500
});
//再次授权,调用getLocationt的API
that.handleLoacation()
} else {
let msg = '授权失败'
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'), msg)
this.$u.toast(msg)
}
}
})
}
}
})
},