65 lines
1.9 KiB
JavaScript
65 lines
1.9 KiB
JavaScript
import { $mapKey } from "@/http/apiManager.js"
|
|
import address from "@/components/applyJson/address.json" // 地址
|
|
|
|
/***
|
|
* 使用 Promise 风格对逆地理编码进行封装
|
|
* 入参 address 地理位置 type0地区码 1 经纬度
|
|
*
|
|
* */
|
|
export const getMapKey = (address, type) => {
|
|
return new Promise((res, rej) => {
|
|
$mapKey()
|
|
.then(({ bizData }) => {
|
|
return bizData.apiMapWebServiceKey
|
|
})
|
|
.then((key) => {
|
|
return new Promise((res, rej) => {
|
|
uni.request({
|
|
url: "https://restapi.amap.com/v3/geocode/geo", // 调用高德接口
|
|
data: {
|
|
key,
|
|
address,
|
|
},
|
|
success: ({ data }) => {
|
|
switch (type) {
|
|
case 0:
|
|
if (data.status == 1) return res({ code: data.geocodes[0].adcode, text: data.geocodes[0].district })
|
|
break
|
|
case 1:
|
|
if (data.status == 1)
|
|
return res({
|
|
lat: data.geocodes[0].location.split(",")[1],
|
|
lon: data.geocodes[0].location.split(",")[0],
|
|
flag: true,
|
|
})
|
|
break
|
|
}
|
|
console.log("res", data)
|
|
},
|
|
fail: (err) => {
|
|
console.log(err, "error")
|
|
},
|
|
})
|
|
})
|
|
})
|
|
.then((val) => {
|
|
if (val.flag) res(val)
|
|
res(inverse(val.code, val.text))
|
|
})
|
|
})
|
|
}
|
|
|
|
const inverse = (code, text) => {
|
|
let lastReg = new RegExp(code) // 将要用于正则匹配
|
|
let result = address.find((item) => {
|
|
// 获取省级信息
|
|
return lastReg.test(JSON.stringify(item))
|
|
})
|
|
|
|
let secondContent = result.children.find((item) => {
|
|
// 获取市级信息
|
|
return lastReg.test(JSON.stringify(item))
|
|
})
|
|
return { code: [result.value, secondContent.value, code], text: [result.text, secondContent.text, text].join("/") }
|
|
}
|