<view @click="toCommunity">点击选择位置</view>
toCommunity() {const that = thisuni.getSetting({success: (res) => {const status = res.authSetting// 如果当前设置是:不允许,则需要弹框提醒客户,需要前往设置页面打开授权if(!status['scope.userLocation']) {uni.showModal({title: "是否授权当前位置",content: "需要获取您的地理位置,请确认授权,否则地图功能将无法使用",success: (tip) => {if (tip.confirm) {// 如果已经拒绝过,则需要打开设置页面,授权弹框不会弹出第二次,因为已经明确拒绝/确认过了(微信的原因)if (that.isDeny) {wx.openSetting({success: function(res) {// 在设置页面授权成功后再次获取位置信息uni.showToast({title: "授权成功",})that.isDeny = false// 修改授权后返回页面,弹框消失,需要再点一次},fail: (data) => {console.log(data)// isDeny 是否拒绝过授权,如果拒绝过,再点击按钮的话,弹框确认后就直接打开微信小程序设置页that.isDeny = true}})return}// 如果点击了确认,并且没有拒绝过微信系统授权弹框,则会弹出授权位置信息的弹框uni.authorize({scope: "scope.userLocation",success: (data) => {// 授权弹框点击确认console.log(data)// 如果用户同意授权,则打开授权设置页面,判断用户的操作uni.openSetting({success: (data) => {// 如果用户授权了地理信息在,则提示授权成功if (data.authSetting['scope.userLocation'] === true) {uni.showToast({title: "授权成功",icon: "none",duration: 1000})}}})},fail: (data) => {// 如果用户拒绝授权,则提示用户需要授权uni.showToast({title: "您已拒绝授权,请重新授权",icon: "none",duration: 1000})that.isDeny = true}})}}})} else {uni.chooseLocation({success: (res) => {that.formData.village = res.namethat.formData.address = res.addressthat.formData.longitude = res.longitudethat.formData.latitude = res.latitudeconst map = new amapFile.AMapWX({key: 'f037f0a9966f01339818bbe2ec1c6495',})map.getRegeo({location: res.longitude + ',' + res.latitude,success: (data) => {this.formData.sheng = data[0].regeocodeData.addressComponent.provincethis.formData.shi = data[0].regeocodeData.addressComponent.citythis.formData.qu = data[0].regeocodeData.addressComponent.district},})},fail: () => {}})}},fail: () => {that.isDeny = true},})},
下方列表可以选择地址,选择后点击右上角完成,会返回页面
二
toCommunity() {uni.getSetting({success: function(res) {if (!res.authSetting['scope.userLocation']) { // 如果没有授权定位if (that.isFirstTime) { // 如果是第一次尝试// 弹出提示框询问用户是否授权uni.showModal({title: '提示',content: '需要获取您的地理位置信息',success: function(modalRes) {if (modalRes.confirm) {// 用户点击了确认,尝试请求授权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)}})},openSetting() {// 打开设置页面uni.openSetting({success: function(res) {if (res.authSetting['scope.userLocation']) {// 用户在设置页面打开了定位权限this.openMap()}},fail: function(err) {// 打开设置页面失败的处理console.error('打开设置页面失败:', err)}})},openMap() {uni.chooseLocation({success: (res) => {console.log('用户选择的地址:', res)this.formData.village = res.namethis.formData.address = res.addressthis.formData.longitude = res.longitudethis.formData.latitude = res.latitudeconst map = new amapFile.AMapWX({key: 'f037f0a9966f01339818bbe2ec1c6495',})map.getRegeo({location: res.longitude + ',' + res.latitude,success: (data) => {this.formData.sheng = data[0].regeocodeData.addressComponent.provincethis.formData.shi = data[0].regeocodeData.addressComponent.citythis.formData.qu = data[0].regeocodeData.addressComponent.district},})},fail: (err) => {console.log('选择位置失败:', err)}})},