import AMapLoader from '@amap/amap-jsapi-loader'; let Amap: any | null | undefined = null; /** * 获取Map对象 * @returns */ export async function GetGDMap() { if (!Amap) { const _window = window as any; _window._AMapSecurityConfig = { securityJsCode: '275e59edb26ab1f917cfe7f05bea7979', }; Amap = await AMapLoader.load({ key: '55a4807a6fc3adca5510f69578b1ae4a', // 申请好的Web端开发者Key,首次调用 load 时必填 version: '1.4.2', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15 plugins: [ 'AMap.ToolBar', 'AMap.Geolocation', 'AMap.Geocoder', 'AMap.MarkerClusterer', 'AMap.Autocomplete', ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等 AMapUI: { version: '1.1', // AMapUI 缺省 1.1 plugins: ['misc/PathSimplifier', 'overlay/SimpleMarker'], //SimpleMarker设置自定义图标,PathSimplifier轨迹展示组件 }, }); } return Amap; } /** * 根据地址查询经纬度 * @param address 地址 * @returns */ export async function GetLocation(address: string) { return new Promise((resolve, reject) => { GetGDMap().then((map) => { const aMap = new map.Geocoder({ radius: 500, // 范围,默认:500 }); aMap.getLocation(address, (status: string, result: any) => { if (status === 'complete' && result.geocodes.length) { resolve({ lng: result.geocodes[0].location.lng, lat: result.geocodes[0].location.lat, }); } else { // eslint-disable-next-line prefer-promise-reject-errors reject('根据经纬度查询地址失败'); } }); }); }); } export default { GetGDMap, GetLocation };