2023-06-29 22:26:35 +08:00

48 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AMapLoader from "@amap/amap-jsapi-loader";
import "@amap/amap-jsapi-types";
import { ElMessage } from "element-plus";
import { AMAP_MAP_KEY } from "@/config/config";
// FIXME: 插件需要配合密钥使用,官方推荐 key 搭配代理服务器并携带安全密钥转发使用
// see: https://lbs.amap.com/api/jsapi-v2/guide/abc/prepare
window._AMapSecurityConfig = {
securityJsCode: "df19aa4c9f83a52f7ef66843449f438e" // 密钥
};
const amap = (async function (Loader, AMAP_MAP_KEY) {
return await Loader.load({
key: AMAP_MAP_KEY,
version: "2.0",
plugins: ["AMap.PlaceSearch", "AMap.AutoComplete", "AMap.Geocoder"]
});
})(AMapLoader, AMAP_MAP_KEY);
// let amap = new Promise((resolve, reject) => {
// let responseData = AMapLoader.load({
// key: AMAP_MAP_KEY,
// version: "2.0",
// plugins: ["AMap.PlaceSearch", "AMap.AutoComplete", "AMap.Geocoder"]
// });
// resolve(responseData);
// });
/**
* @description: 初始化 AMap等待地图加载完成
* @param {InitCallback} callback 初始化回调
*/
async function initAMap() {
try {
const AMap = await amap;
// const AMap = AMapLoader.load({
// key: AMAP_MAP_KEY,
// version: "2.0",
// plugins: ["AMap.PlaceSearch", "AMap.AutoComplete", "AMap.Geocoder"]
// });
return AMap;
} catch (err) {
console.log(err);
ElMessage.error("加载高德地图失败, 请检查网络或需求客服帮助。");
}
}
export default initAMap;