一、下载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);
},
});
}