微信小程序地址获取,不涉及商业版权获取当前位置信息

  1. 获取用户当前小程序设置,判断是否授权,若没有授权,则弹出设置选项让用户进行授权
     againGetLocation() {
          let pages = getCurrentPages() // 当前页面
          const that = this
          wx.getSetting({
            success: (res) => {
              if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
                wx.showModal({
                  title: '是否授权当前位置',
                  content: 'xxxxx',
                  success: function (res) {
                    if (res.cancel) {
                      that.setData({
                        isshowCIty: false
                      })
                      wx.showToast({
                        title: '授权失败',
                        duration: 1000
                      })
                    } else if (res.confirm) {
                      wx.openSetting({
                        success: function (dataAu) {
                          if (dataAu.authSetting['scope.userLocation'] == true) {
                            wx.showToast({
                              title: '授权成功',
                              icon: 'success',
                              duration: 1000
                            })
                            //再次授权,调用getLocationt的API
                            this.getNowCity() // 获取当前定位
                          } else {
                            wx.showToast({
                              title: '授权失败',
                              duration: 1000
                            })
                          }
                        }
                      })
                    }
                  }
                })
              } else if (res.authSetting['scope.userLocation'] === undefined) {//初始化进入
                this.getNowCity() // 获取当前定位
              }
              else  { //授权后默认加载
                this.getNowCity() // 获取当前定位
              }
            }
          })
        }

  2. 授权后调用微信getLocation接口获取位置信息
    getNowCity(){
          const that = this
          // 获取经纬度
          wx.getLocation({
            type: 'wgs84',
            title: '是否授权当前位置',
            content: '需要获取您的地理位置,请确认授权,否则无法获取您所需数据',
            success: function (res) {
              that.nowlocation.latitude = res.latitude,
              that.nowlocation.longitude = res.longitude
              that.$store.commit('UPDATE_LOCATION', that.nowlocation)
              that.getDetail()
            },
            fail: function (res) {
              wx.showToast({
                title: '授权失败',
                duration: 1000
              })
            }
          })
        }

  3. 获取位置信息后,使用当前经纬度获取具体位置信息,不涉及地图商业授权
    // 获取定位城市信息
        getDetail(){
          const that = this
          wx.chooseLocation({
            latitude: that.nowlocation.latitude, // 上一步操作获取的经纬度
            longitude: that.nowlocation.longitude,
            success (res) {
              const { address, latitude, longitude } = res
              const regex = '(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)';
              const address2 = address.match(regex) // 根据省、市、区重组数组
              that.cityInfo.cityName = address2[2] // 当前城市
              that.nowlocation.latitude = latitude
              that.nowlocation.longitude = longitude
              that.$store.commit('UPDATE_NOWCITY', that.cityInfo)
              that.$store.commit('UPDATE_LOCATION', that.nowlocation)
            },
            fail (res) {
              //console.log(res)
            }
          })
        },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取微信小程序当前位置的详细信息,你使用微信小程序提供的`wx.getLocation接口获取经纬度,然后使用地理逆编码服务将经度转换为详细地址信息。以下是示例代码: ```javascript // 在小程序的页面中调用获取位置的方法 wx.getLocation({ type: 'wgs84', // 返回经纬度信息的类型,这里使用wgs84 success: function(res) { var latitude = res.latitude; // 纬度 var longitude = res.longitude; // 经度 // 调用腾讯地图的逆地址解析接口,将经纬度转换为详细地址 wx.request({ url: 'https://apis.map.qq.com/ws/geocoder/v1', data: { location: latitude + ',' + longitude, key: 'your_tencent_map_api_key', get_poi: 1 }, success: function(res) { var address = res.data.result.address; // 详细地址信息 var pois = res.data.result.pois; // 附近的POI信息 // 在这里处理获取到的位置信息 console.log("详细地址:" + address); console.log("附近POI:" + JSON.stringify(pois)); }, fail: function(res) { // 处理失败情况 console.log("逆地址解析失败:" + res.errMsg); } }); }, fail: function(res) { // 处理失败情况 console.log("获取位置失败:" + res.errMsg); } }); ``` 在成功回调函数中,我们发送了一个HTTP请求给腾讯地图的逆地址解析接口,将获取到的经纬度信息传递给接口,同时提供你的腾讯地图API密钥。接口会返回详细的地址信息和附近的POI(兴趣点)信息。你可以根据需求处理这些信息。 请注意,为了使用腾讯地图的服务,你需要在小程序后台设置中申请并获取到腾讯地图API密钥,并将其替换到示例代码中的`your_tencent_map_api_key`处。 希望对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值