二次封装高德地图常用方法,便于开发
const { AMap } = window;
export const mapInit = (domId, options = {}) => {
return new Promise((resolve, reject) => {
const map = new AMap.Map(domId, {
resizeEnable: true,
...options,
});
resolve(map);
});
};
export const drawCircle = ({ lat, lng, radius = 3000 }) => {
return new AMap.Circle({
center: [lng, lat],
radius,
borderWeight: 1,
strokeColor: '#cdc8b1',
strokeWeight: 1,
strokeOpacity: 0.2,
strokeStyle: 'dashed',
strokeDasharray: [10, 10],
fillColor: '#999999',
fillOpacity: 0.2,
zIndex: 50,
});
};
export const drawHeatMap = (map, heatmapData) => {
return new Promise((resolve, reject) => {
map.plugin(['AMap.HeatMap'], () => {
const heatmap = new AMap.HeatMap(map, {
radius: 25,
opacity: [0, 0.8],
zIndex: 49,
});
heatmap.setDataSet({
data: heatmapData,
});
resolve(heatmap);
});
});
};
export const addMarker = (map, item, options = {}) => {
return new Promise((resolve, reject) => {
const marker = new AMap.Marker({
map,
anchor: 'bottom-center',
position: [item.point_x, item.point_y],
...options,
});
resolve(marker);
});
};
export const drawPolygon = (path) => {
return new AMap.Polygon({
path,
strokeColor: '#c6b389',
strokeStyle: 'dashed',
strokeWeight: 2,
strokeOpacity: 1,
fillOpacity: 0.4,
fillColor: '#f0d8ac',
zIndex: 50,
});
};
export const addLabelMarker = ({
lat,
lng,
zIndex = 9,
text = {},
icon = {},
visible = true,
}) => {
return new AMap.LabelMarker({
position: [lng, lat],
zIndex,
visible,
icon,
});
};
export const drawLabelsLayer = ({ zIndex = 2000, text = {} }) => {
return new AMap.LabelsLayer({
zooms: [3, 20],
zIndex,
collision: false,
allowCollision: false,
});
};
export const textStyle = {
fontSize: 10,
fontWeight: 'PingFangSC-Regular',
fillColor: '#fff',
padding: '2, 2',
backgroundColor: 'rgb(103,156,222)',
};
export const centerPointGetFourPoint = ({ lat, lng }, side = 1000) => {
const centerPoint = new AMap.LngLat(lng, lat);
const upLeftPoint = centerPoint.offset(-side / 2, side / 2);
const upRightPoint = centerPoint.offset(side / 2, side / 2);
const leftBottomPoint = centerPoint.offset(-side / 2, -side / 2);
const rightBottomPoint = centerPoint.offset(side / 2, -side / 2);
return {
upLeftPoint: [upLeftPoint.lng, upLeftPoint.lat],
upRightPoint: [upRightPoint.lng, upRightPoint.lat],
leftBottomPoint: [leftBottomPoint.lng, leftBottomPoint.lat],
rightBottomPoint: [rightBottomPoint.lng, rightBottomPoint.lat],
};
};