一、前言
目的:为了获取详细地址中文
二、问题
// 前人书写的代码:
const url = `https://restapi.amap.com/v3/geocode/regeo?location=${经纬度}&key=${绑定平台的key}`
const [error, res] = await uni.request({ url });
// 这样会出现 如下情况
/**
* info: "USERKEY_PLAT_NOMATCH"
* infocode: "10009"
* status: "0"
*/
三、寻求解决方法
1、微信小程序相关插件下载 提供在微信小程序中使用高德、客户业务数据的功能。
2、导入到项目
3、开始梭哈代码
/**
* amapTools.js 文件
*/
import amap from "@/js_sdk/amap/amap-wx.130.js"; // 下载的插件存放位置
import { MAP_KEY } from "@/env.js"; // 存放高德key的目录
const Amap = new amap.AMapWX({
key: MAP_KEY
});
export default Amap;
4、在vue文件中调用
<script>
import Amap from "@/common/utils/amapTools";
export default {
data() {
Amap,
// 自行替换经纬度
lng: "xxx", // 自行替换
lat: "xxx", // 自行替换
},
methods: {
getAddress() {
// 获取位置信息
this.Amap.getRegeo({
location: `${this.lng},${this.lat}`,
success: (res) => {
// console.log("res -0-0", res);
let data = res[0].regeocodeData.addressComponent;
},
fail: (err) => {
// console.log("err-0-0", err);
},
});
},
/**
* searchVal: 搜索值
* cityCode:城市code 相关参考可以查阅
* https://lbs.amap.com/api/webservice/guide/api-advanced/inputtips
*/
inputTips(searchVal, cityCode) {
// 地点搜索
this.Amap.getInputtips({
keywords: searchVal,
city: cityCode,
citylimit: true,
location: `${this.lng},${this.lat}`,
success: (res) => {
// console.log("getInputtips -0-0 res", res);
},
fail: (err) => {
// console.log("getInputtips -0-0 err", err);
},
});
}
}
}
</script>
5、如此这般之后,成功跑通
{info: "OK",infocode: "10000",status: "1"}
四、结尾
如有打错的地方,请高抬贵手