获取当前位置根据当前位置的经纬度转换成详细地址

1.在app.json/uniapp.json中声明permission

{
    "pages": [
        "pages/home/index",
        "pages/promote/index",
        "pages/partner/index/index",
        "pages/my/index",
        "pages/login/wxmp"
    ],
    "permission":{
        "scope.userLocation":{
            "desc":"获取当前位置"
        }
    },
}

2. 首先调用wx.getLocation获得当前位置经纬度

2.1腾讯官网:https://lbs.qq.com/

2.1.2如何申请key:https://baijiahao.baidu.com/s?id=1622891654704256153&wfr=spider&for=pc

2.1.3一定要在小程序后台把域名加到服务器域名中否则真机调试获取不到详细地址:

  • https://apis.map.qq.com

ps:之所以前缀是uni开头是因为我用uniapp开发的,原生开发的直接把uni改成wx

//第一步
 getLocationt: function() {
      const that = this;
      // 首先调用wx.getLocation获得当前位置经纬度
      uni.getLocation({
        type: 'gcj02',
        success: function(res) {
          // 腾讯api:https://apis.map.qq.com/ws/geocoder/v1/。key要自己去申请
          var url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + res.latitude + ',' + res.longitude + '&key=dlskdlsf ';
          uni.request({
            url: url,
            success: (result) => {
              that.applyaddress = result.data.result.address;
              console.log('address', that.applyaddress);
            }, fail(res) {
              console.log('失败了', res, url);
            },
          });
        },
      });
    },

//第二步
 // 授权失败重新调用
    openlocation() {
      var that = this;
      uni.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userLocation'] !== undefined && res.authSetting['scope.userLocation'] !== true) { // 非初始化进入该页面,且未授权
            uni.showModal({
              title: '授权当前位置',
              content: '需要获取您的当前位置,
              success: function(res) {
                if (res.cancel) {
                  uni.showToast({
                    title: '授权当前位置失败,请重新授权',
                    icon: 'none',
                  });
                } else if (res.confirm) {
                  uni.openSetting({
                    success: function(dataAu) {
                      if (dataAu.authSetting['scope.userLocation'] === true) {
                        // 再次授权,调用getLocationt的API
                        that.getLocationt();
                      }
                    },
                  });
                }
              },
            });
          } else if (res.authSetting['scope.userLocation'] === undefined) { // 初始化进入
            that.getLocationt();
          } else { // 授权后默认加载
            that.getLocationt();
          }
        },
      });
    },

3.效果图:address获取到的地址输出

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值