uniapp.getLocation 获取当前经纬度以及逆地理解析

  getPositioning() {
      let that = this;
      uni.getLocation({
        type: "gcj02", //默认为 wgs84 返回 gps 坐标
        geocode: "true",
        isHighAccuracy: true,
        // accuracy: "best", // 精度值为20m
        success: function (res) {
          console.log("定位获取:", res);
          console.log("经纬度: ", res.latitude, res.longitude)
          // 逆地理解析
          uni.request({
            url: 'https://restapi.amap.com/v3/geocode/regeo',
            method: 'GET',
            data: {
              location: res.longitude + ',' + res.latitude,
              key: '高德key', //  web服务,其他的不行
            },
            success: function (res) {
              that.address = res.data.regeocode.formatted_address
              console.log(res.data.regeocode.formatted_address);
            },
            fail: function (err) {
              console.log('地址解析失败' + err);
            }
          })

        },
        fail(err) {
          if (
            err.errMsg ===
            "getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化"
          ) {
            uni.showToast({
              title: "请勿频繁定位",
              icon: "none",
            });
          }
          if (err.errMsg === "getLocation:fail auth deny") {
            // 未授权
            uni.showToast({
              title: "无法定位,请重新获取位置信息",
              icon: "none",
            });
            authDenyCb && authDenyCb();
            that.isLocated = false;
          }
          if (
            err.errMsg ===
            "getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"
          ) {
            uni.showModal({
              content: "请开启手机定位服务",
              showCancel: false,
            });
          }
          console.log(err.errMsg, '111')
        },
      });
    },

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp中进行经纬度地址解析可以使用第三方地图API,例如高德地图、百度地图等。以下是使用高德地图API进行地址解析的示例代码: 1. 在uniapp项目中安装axios库,可以使用以下命令进行安装: ``` npm install axios ``` 2. 在uniapp项目中创建一个api.js文件,用于封装调用高德地图API的方法。示例代码如下: ``` import axios from 'axios'; const KEY = 'your_amap_key'; // 替换为自己的高德地图API Key // 根据经纬度获取地址解析信息 export function getAddressByLocation(location) { return axios.get(`https://restapi.amap.com/v3/geocode/regeo?output=json&location=${location}&key=${KEY}&radius=1000&extensions=all`); } ``` 3. 在需要进行地址解析的页面中,引入api.js文件,并调用getAddressByLocation方法进行调用。示例代码如下: ``` import { getAddressByLocation } from '@/api'; uni.getLocation({ success: function(res) { // 获取经度和纬度 var longitude = res.longitude; var latitude = res.latitude; console.log('经度:' + longitude); console.log('纬度:' + latitude); // 调用高德地图API进行地址解析 getAddressByLocation(longitude + ',' + latitude).then(res => { console.log('地址解析结果:', res.data); // 在页面上显示地址信息 this.address = res.data.regeocode.formatted_address; }).catch(err => { console.log('地址解析失败:', err); }); }, fail: function(res) { console.log('获取位置信息失败'); } }); ``` 在上述代码中,通过getLocation方法获取经纬度信息,然后调用getAddressByLocation方法进行地址解析,最后将解析结果显示在页面上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值