uniapp开发微信小程序,选择地理位置uni.chooseLocation

<view @click="toCommunity">点击选择位置</view>
    toCommunity() {
      const that = this
      uni.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.name
                that.formData.address = res.address
                that.formData.longitude = res.longitude
                that.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: () => {
              }
            })
          }
        },
        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.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)
        }
      })
    },
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值