<view @click="toCommunity"></view>
// 选择小区
toCommunity() {
const that = this
// 同伙getSettingAPI获取授权状态
// 如果未授权且是第一次点击,则需要弹框提示授权
// 点击弹框确认按钮,则调用authorize,弹出微信内置授权弹框
// 如果已经拒绝过授权了,就无需弹框提示,直接打开设置页面
// 设置页面返回后,可以使用success/fail回调,判断用户是否授权
// 如果已经授权,则直接调用openMap前往选择地址
// 如果未授权,则不需要操作
// 如果已经授权,则直接调用openMap前往选择地址
uni.getSetting({
success: function(res) {
if (!res.authSetting['scope.userLocation']) { // 如果没有授权定位
if (that.isFirstTime) { // 如果是第一次尝试
// 弹出提示框询问用户是否授权
uni.showModal({
title: '提示',
content: '需要获取您的地理位置信息',
success: function(modalRes) {
if (modalRes.confirm) {
console.log('用户点击了确认')
// 用户点击了确认,尝试请求授权
uni.authorize({
scope: 'scope.userLocation',
success: function() {
// 授权成功,调用openMap方法
that.openMap()
},
fail: function() {
// 授权失败,可能是用户拒绝了,此时可以打开设置页面
that.openSetting()
}
})
}
}
})
that.$store.commit('updateIsFirstTime', false) // 更新为已尝试授权
} else {
// 用户之前已经拒绝过授权,直接打开设置页面
that.openSetting()
}
} else {
// 已经授权,直接调用openMap方法
that.openMap()
}
},
fail: function(err) {
// 获取设置失败的处理
console.error('获取用户设置失败:', err)
that.$u.toast('获取城市信息失败,请打开设置页面修改')
}
})
},
openSetting() {
const that = this
// 打开设置页面
uni.openSetting({
success: function(res) {
if (res.authSetting['scope.userLocation']) {
// 用户在设置页面打开了定位权限
this.openMap()
}
},
fail: function(err) {
// 打开设置页面失败的处理
console.error('打开设置页面失败:', err)
that.$u.toast('获取城市信息失败,请打开设置页面修改')
}
})
},
openMap() {
uni.chooseLocation({
success: (res) => {
console.log('用户选择的地址:', res)
this.formData.village = res.name
this.formData.address = res.address
this.formData.longitude = res.longitude
this.formData.latitude = res.latitude
const map = new amapFile.AMapWX({
key: 'f037f0a9966f01339818bbe2ec1c6495',
})
map.getRegeo({
location: res.longitude + ',' + res.latitude,
success: (data) => {
this.formData.sheng = data[0].regeocodeData.addressComponent.province
this.formData.shi = data[0].regeocodeData.addressComponent.city
this.formData.qu = data[0].regeocodeData.addressComponent.district
},
})
},
fail: (err) => {
console.log('选择位置失败:', err)
}
})
},
03-29
655
10-15
2852
07-25
5291
05-29
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交