uniapp使用高德地图解析经纬度转为中文地址

一、下载amap-wx.js

下载地址:https://lbs.amap.com/api/wx/download
获取文件如下:
在这里插入图片描述
将amap-wx.js复制到自己的项目中。

二、引入微信jsdk
<script lang='ts'>
import { Component, Vue } from "vue-property-decorator";
import _config from "@/config.ts";
var amapFile = require('@/utils/amap-wx.js'),//引入刚刚下载的文件
		//设置初始化
		markersData = {
		  latitude: '',//纬度
		  longitude: '',//经度
		  key: "xxxxxxxxxxxxxxx"//申请的高德地图key
		}
@Component({})

Tips:这里key需要自己在高德地图申请,申请方法不做过多描述,网上一搜就有很多,对应的key值需要对应的服务。比如:我是微信小程序应用到的,所以就必须选择微信小程序
在这里插入图片描述

三、使用
getLocation() {
    this.getSetting();
    this.doGetLocation();
  }

  /* 是否授权,没有授权则授权 */
  getSetting() {
    console.log(1)
    return new Promise((resolve, reject) => {
      uni.getSetting({
        success: (res) => {
          if (res.authSetting["scope.userLocation"] === undefined) {
            resolve(0);
            return;
          }
          if (res.authSetting["scope.userLocation"]) {
            resolve(1);
          } else {
            resolve(2);
          }
        },
      });
    });
  }

  //获取经纬度
  doGetLocation() {
    uni.getLocation({
      type: "wgs84",
      success: (res) => {
        this.getCity(res.latitude, res.longitude);
      },
      fail: (err) => {
        uni.showToast({
          title: "获取失败",
        });
      },
    });
  }

	//解析经纬度
  getCity(latitude: any, longitude: any) {
    console.log(latitude,longitude)
    let that = this;
    var myAmapFun = new amapFile.AMapWX({ key: markersData.key });
    myAmapFun.getRegeo({
      location: "" + longitude + "," + latitude + "", //location的格式为'经度,纬度'
      success: function (e: any) {
       that.city = e[0].regeocodeData.addressComponent.city; //城市
        that.province = e[0].regeocodeData.addressComponent.province; //省份
        console.log(that.city,that.province)
      },
      fail: function (info:any) {
        console.log(info);
      },
    });
  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值