import AMapLoader from '@amap/amap-jsapi-loader'; export function useAMap(options) { let AMap, map, nowLayer, labelsLayer, scale, cluster; const markers = { point: [] }; // 初始化事件 const initMap = (options) => { AMapLoader.load({ key: options.key, // 申请好的Web端开发者Key,首次调用 load 时必填 version: !!options.version ? options.version : '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15 plugins: options.plugins ? options.plugins : ['AMap.Scale', 'AMap.RangingTool', 'AMap.MouseTool', 'AMap.PolygonEditor', 'AMap.MarkerCluster'] }).then((res) => { AMap = res; map = new AMap.Map('aMap', { // 是否为3D地图模式 viewMode: '3D', pitch: options.pitch, // 初始化地图级别 zoom: options.zoom ? options.zoom : 11, // 初始化地图中心点位置 center: options.center, // 是否可拖拽 dragEnable: options.dragEnable, // 是否允许通过鼠标滚轮来缩放 scrollWheel: options.scrollWheel }); // 初始化比例尺 if (options.showScale) { scale = new AMap.Scale(); map.addControl(scale); } if (typeof options.onLoadCompleted === 'function') { options.onLoadCompleted(AMap, map); } }); }; const getAMap = () => { return AMap; }; const getMap = () => { return map; }; const getScale = () => { return scale; }; // 切换地图 const switchMap = (type: string) => { if (type === 'vectorgraph') { map.removeLayer(nowLayer); } else if (type === 'satellite') { const satellite = new AMap.TileLayer.Satellite(); map.addLayer(satellite); nowLayer = satellite; } }; // 添加搜索的标记的 const addSearchMarker = (item) => { map.setCenter([item.longitude, item.latitude]); addMarker(item, true); }; const addMarker = (points, isSearchItem?: boolean) => { clearMarker('point'); const count = points.length; const _renderClusterMarker = function (context) { console.log(context); console.log('21312', context.clusterData); // 聚合中点个数 const clusterCount = context.count; const div = document.createElement('div'); div.style.backgroundColor = 'rgba(78,179,211,.5)'; const size = Math.round(25 + Math.pow(clusterCount / count, 1 / 5) * 20); div.style.width = div.style.height = size + 'px'; div.style.border = 'solid 1px rgba(78,179,211,1)'; div.style.borderRadius = size / 2 + 'px'; div.innerHTML = context.count; div.style.lineHeight = size + 'px'; div.style.color = '#ffffff'; div.style.fontSize = '12px'; div.style.textAlign = 'center'; context.marker.setOffset(new AMap.Pixel(-size / 2, -size / 2)); context.marker.setContent(div); context.marker.on('click', (e) => { debugger; }); }; const _renderMarker = function (context) { const content = '