|
@@ -10,7 +10,7 @@ interface DrawToolOptions {
|
|
|
graphicsType: string;
|
|
|
onDrawCompleted?: Function;
|
|
|
}
|
|
|
-export function useDrawTool(options: DrawToolOptions) {
|
|
|
+export function useDrawTool() {
|
|
|
let drawOptions = {
|
|
|
strokeColor: '#f80102',
|
|
|
strokeOpacity: 1,
|
|
@@ -22,7 +22,6 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
const { currentState, commit, undo, history, future } = useHistory();
|
|
|
const overlays = [];
|
|
|
const overlaysData = [];
|
|
|
- let graphicsType = options.graphicsType;
|
|
|
let mouseTool, contextMenu, container, map, AMap, rightClickObj;
|
|
|
const initMouseTool = (options2) => {
|
|
|
container = document.getElementById('aMap');
|
|
@@ -32,59 +31,15 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
mouseTool = new AMap.MouseTool(map);
|
|
|
//创建右键菜单
|
|
|
contextMenu = new AMap.ContextMenu();
|
|
|
- // 右键删除按钮
|
|
|
- contextMenu.addItem(
|
|
|
- '删除',
|
|
|
- function () {
|
|
|
- deleteGraphics();
|
|
|
- },
|
|
|
- 0
|
|
|
- );
|
|
|
- };
|
|
|
- // 初始化绘制完成回调
|
|
|
- const initDrawMethod = (drawOptions) => {
|
|
|
- mouseTool.on('draw', function (event) {
|
|
|
- const obj = event.obj;
|
|
|
- const id = nanoid();
|
|
|
- obj._opts.extData = {
|
|
|
- id: id
|
|
|
- };
|
|
|
- const data: any = deepClone(drawOptions);
|
|
|
- data.id = id;
|
|
|
- if (drawOptions.type == 'marker') {
|
|
|
- data.path = obj.getPosition();
|
|
|
- } else {
|
|
|
- const path = obj.getPath();
|
|
|
- // 将AMap.LngLat对象数组转换为经纬度数组
|
|
|
- const pathArr = path.map((lngLat) => {
|
|
|
- // 返回经度和纬度的数组
|
|
|
- return [lngLat.lng, lngLat.lat];
|
|
|
- });
|
|
|
- pathArr.push(pathArr[0]);
|
|
|
- data.path = pathArr;
|
|
|
- }
|
|
|
- overlays.push(obj);
|
|
|
- overlaysData.push(data);
|
|
|
- commit(deepClone(overlaysData));
|
|
|
- debugger
|
|
|
- if (typeof options.onDrawCompleted === 'function') {
|
|
|
- options.onDrawCompleted(data, overlaysData, obj, event);
|
|
|
- }
|
|
|
- // 右击菜单
|
|
|
- obj.on('rightclick', handleRightClick);
|
|
|
- });
|
|
|
- };
|
|
|
- // 图形右击事件
|
|
|
- const handleRightClick = (event) => {
|
|
|
- rightClickObj = event.target;
|
|
|
- contextMenu.open(map, event.lnglat);
|
|
|
};
|
|
|
+
|
|
|
// 删除图形
|
|
|
const deleteGraphics = () => {
|
|
|
const id = rightClickObj.getExtData()?.id;
|
|
|
if (id) {
|
|
|
for (let i = 0; i < overlays.length; i++) {
|
|
|
- if (overlays[i].getExtData()?.id === id) {
|
|
|
+ const overlay = Array.isArray(overlays[i]) ? overlays[i][0] : overlays[i];
|
|
|
+ if (overlay?.getExtData().id === id) {
|
|
|
removeOverlayByIndex(i);
|
|
|
commit(deepClone(overlaysData));
|
|
|
rightClickObj = null;
|
|
@@ -119,7 +74,7 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
anchor: 'bottom-center',
|
|
|
title: newOptions.title,
|
|
|
icon: newOptions.icon,
|
|
|
- size: new AMap.Size(newOptions.size[0], newOptions.size[1])
|
|
|
+ size: [newOptions.size[0], newOptions.size[1]]
|
|
|
};
|
|
|
} else if (newOptions.graphicsType === 'text') {
|
|
|
drawOptions = {
|
|
@@ -147,24 +102,44 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
mouseTool.polyline(drawOptions);
|
|
|
} else if (newOptions.graphicsType === 'text') {
|
|
|
// 绘制文字
|
|
|
- addText(drawOptions);
|
|
|
+ return addText(drawOptions);
|
|
|
} else if (newOptions.graphicsType === 'marker') {
|
|
|
// 绘制图标
|
|
|
mouseTool.marker(drawOptions);
|
|
|
} else if (newOptions.graphicsType === 'measureArea') {
|
|
|
// 测量面积
|
|
|
- mouseTool.measureArea(drawOptions);
|
|
|
+ mouseTool.polygon(drawOptions);
|
|
|
}
|
|
|
- if (newOptions.graphicsType !== 'text') {
|
|
|
- // 绘制完成事件
|
|
|
- initDrawMethod(drawOptions);
|
|
|
+ return drawOptions;
|
|
|
+ };
|
|
|
+ // 空间分析绘制图形
|
|
|
+ const drawGraphics2 = (newOptions: MouseTool) => {
|
|
|
+ drawOptions = {
|
|
|
+ type: newOptions.graphicsType,
|
|
|
+ strokeColor: newOptions.color,
|
|
|
+ strokeOpacity: 1,
|
|
|
+ strokeWeight: '1',
|
|
|
+ fillColor: newOptions.color,
|
|
|
+ fillOpacity: newOptions.drawType === '1' ? 0 : 0.5,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ };
|
|
|
+ closeDraw();
|
|
|
+ if (newOptions.graphicsType === 'circle') {
|
|
|
+ // 绘制圆形
|
|
|
+ mouseTool.circle(drawOptions);
|
|
|
+ } else if (newOptions.graphicsType === 'rectangle') {
|
|
|
+ // 绘制矩形
|
|
|
+ mouseTool.rectangle(drawOptions);
|
|
|
+ } else if (newOptions.graphicsType === 'polygon') {
|
|
|
+ // 绘制多边形
|
|
|
+ mouseTool.polygon(drawOptions);
|
|
|
}
|
|
|
};
|
|
|
- const addText = (drawOptions) => {
|
|
|
+ const addText = (options) => {
|
|
|
// 文本覆盖物的样式
|
|
|
const textStyle = {
|
|
|
- fontSize: drawOptions.fontSize,
|
|
|
- color: drawOptions.fontColor,
|
|
|
+ fontSize: options.fontSize,
|
|
|
+ color: options.fontColor,
|
|
|
borderColor: 'transparent',
|
|
|
backgroundColor: 'transparent',
|
|
|
borderWidth: 0,
|
|
@@ -173,11 +148,11 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
|
|
|
// 创建文本覆盖物
|
|
|
const text = new AMap.Text({
|
|
|
- text: drawOptions.text, // 文本内容,可以根据需要自定义
|
|
|
- position: drawOptions.lnglat, // 文本位置(经纬度)
|
|
|
+ text: options.text, // 文本内容,可以根据需要自定义
|
|
|
+ position: options.lnglat, // 文本位置(经纬度)
|
|
|
style: textStyle, // 文本样式
|
|
|
zIndex: 100, // 文本层级
|
|
|
- draggable: true // 是否可拖动(可选)
|
|
|
+ draggable: false // 是否可拖动(可选)
|
|
|
});
|
|
|
// 将文本覆盖物添加到地图
|
|
|
map.add(text);
|
|
@@ -185,58 +160,50 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
text._opts.extData = {
|
|
|
id: id
|
|
|
};
|
|
|
- const data: any = deepClone(drawOptions);
|
|
|
+ const data: any = deepClone(options);
|
|
|
data.id = id;
|
|
|
- overlays.push(text);
|
|
|
- overlaysData.push(data);
|
|
|
- commit(deepClone(overlaysData));
|
|
|
- console.log(overlaysData);
|
|
|
- if (typeof options.onDrawCompleted === 'function') {
|
|
|
- options.onDrawCompleted(data, overlaysData, text);
|
|
|
- }
|
|
|
- // 右击菜单
|
|
|
- text.on('rightclick', handleRightClick);
|
|
|
+ return { text, data };
|
|
|
};
|
|
|
let polyline = null;
|
|
|
let startPoint = null;
|
|
|
let isDrawing = false;
|
|
|
let lastPoint = null;
|
|
|
// 绘制任意线
|
|
|
- const drawAnyLine = (drawOptions) => {
|
|
|
- container.addEventListener('mousedown', drawAnyLineMouseDown);
|
|
|
-
|
|
|
- container.addEventListener('mousemove', (e) => {
|
|
|
- e.preventDefault();
|
|
|
- if (!isDrawing) return;
|
|
|
-
|
|
|
- const mapPoint = this.map.getContainer().getBoundingClientRect();
|
|
|
- const lngLat = this.map.getLngLatFromContainerPixel([e.clientX - mapPoint.left, e.clientY - mapPoint.top]);
|
|
|
-
|
|
|
- // 更新线条(这里需要手动管理polyline的坐标)
|
|
|
- if (polyline) {
|
|
|
- polyline.setPath(polyline.getPath().concat([lngLat]));
|
|
|
- } else {
|
|
|
- // 首次绘制时创建polyline
|
|
|
- polyline = new AMap.Polyline({
|
|
|
- map: this.map,
|
|
|
- path: [startPoint, lngLat],
|
|
|
- strokeColor: drawOptions.strokeColor,
|
|
|
- strokeWeight: drawOptions.strokeWeight,
|
|
|
- strokeOpacity: drawOptions.strokeOpacity,
|
|
|
- strokeStyle: 'solid',
|
|
|
- lineJoin: 'round',
|
|
|
- lineCap: 'round',
|
|
|
- zIndex: 50
|
|
|
- });
|
|
|
- }
|
|
|
- lastPoint = lngLat;
|
|
|
- console.log(lngLat);
|
|
|
- });
|
|
|
-
|
|
|
- container.addEventListener('mouseup', (e) => {
|
|
|
- isDrawing = false;
|
|
|
- map.setDraggable(true);
|
|
|
- });
|
|
|
+ const drawAnyLine = (options) => {
|
|
|
+ // container.addEventListener('mousedown', drawAnyLineMouseDown);
|
|
|
+ //
|
|
|
+ // container.addEventListener('mousemove', (e) => {
|
|
|
+ // e.preventDefault();
|
|
|
+ // if (!isDrawing) return;
|
|
|
+ //
|
|
|
+ // const mapPoint = this.map.getContainer().getBoundingClientRect();
|
|
|
+ // const lngLat = this.map.getLngLatFromContainerPixel([e.clientX - mapPoint.left, e.clientY - mapPoint.top]);
|
|
|
+ //
|
|
|
+ // // 更新线条(这里需要手动管理polyline的坐标)
|
|
|
+ // if (polyline) {
|
|
|
+ // polyline.setPath(polyline.getPath().concat([lngLat]));
|
|
|
+ // } else {
|
|
|
+ // // 首次绘制时创建polyline
|
|
|
+ // polyline = new AMap.Polyline({
|
|
|
+ // map: this.map,
|
|
|
+ // path: [startPoint, lngLat],
|
|
|
+ // strokeColor: options.strokeColor,
|
|
|
+ // strokeWeight: options.strokeWeight,
|
|
|
+ // strokeOpacity: options.strokeOpacity,
|
|
|
+ // strokeStyle: 'solid',
|
|
|
+ // lineJoin: 'round',
|
|
|
+ // lineCap: 'round',
|
|
|
+ // zIndex: 50
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+ // lastPoint = lngLat;
|
|
|
+ // console.log(lngLat);
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // container.addEventListener('mouseup', (e) => {
|
|
|
+ // isDrawing = false;
|
|
|
+ // map.setDraggable(true);
|
|
|
+ // });
|
|
|
};
|
|
|
// 任意线 鼠标按下事件
|
|
|
const drawAnyLineMouseDown = (e) => {
|
|
@@ -262,19 +229,22 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
const createGraphics = (data: any) => {
|
|
|
if (data.type === 'circle') {
|
|
|
// 绘制圆形
|
|
|
- createCircle(data);
|
|
|
- } else if (data.type === 'rectangle') {
|
|
|
- // 绘制矩形
|
|
|
- createRectangle(data);
|
|
|
- } else if (data.type === 'polygon') {
|
|
|
- // 绘制多边形
|
|
|
- createPolygon(data);
|
|
|
+ return createCircle(data);
|
|
|
+ } else if (['polygon', 'rectangle'].includes(data.type)) {
|
|
|
+ // 绘制矩形、多边形
|
|
|
+ return createPolygon(data);
|
|
|
} else if (data.type === 'marker') {
|
|
|
// 绘制图标
|
|
|
- createMarker(data);
|
|
|
+ return createMarker(data);
|
|
|
} else if (data.type === 'measureArea') {
|
|
|
- // 绘制图标
|
|
|
- createMeasureArea(data);
|
|
|
+ // 绘制面积
|
|
|
+ return createMeasureArea(data);
|
|
|
+ } else if (data.type === 'text') {
|
|
|
+ // 文字
|
|
|
+ return addText(data);
|
|
|
+ } else if (data.type === 'straightLine') {
|
|
|
+ // 直线
|
|
|
+ return createStraightLine(data);
|
|
|
}
|
|
|
};
|
|
|
// 创建圆形
|
|
@@ -282,15 +252,15 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
const circle = new AMap.Circle({
|
|
|
center: options.center,
|
|
|
radius: options.radius, //半径
|
|
|
- strokeColor: options.color,
|
|
|
- strokeOpacity: 1,
|
|
|
- strokeWeight: options.lineWidth,
|
|
|
- fillColor: options.color,
|
|
|
- fillOpacity: options.opacity,
|
|
|
+ strokeColor: options.strokeColor,
|
|
|
+ strokeOpacity: options.strokeOpacity,
|
|
|
+ strokeWeight: options.strokeWeight,
|
|
|
+ fillColor: options.fillColor,
|
|
|
+ fillOpacity: options.fillOpacity,
|
|
|
strokeStyle: 'solid'
|
|
|
});
|
|
|
- overlays.push(circle);
|
|
|
map.add(circle);
|
|
|
+ return circle;
|
|
|
};
|
|
|
// 创建矩形
|
|
|
const createRectangle = (options: any) => {
|
|
@@ -299,17 +269,17 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
const bounds = new AMap.Bounds(southWest, northEast);
|
|
|
const rectangle = new AMap.Rectangle({
|
|
|
bounds: bounds,
|
|
|
- strokeColor: options.color,
|
|
|
- strokeOpacity: 1,
|
|
|
- strokeWeight: options.lineWidth,
|
|
|
- fillColor: options.color,
|
|
|
- fillOpacity: options.opacity,
|
|
|
+ strokeColor: options.strokeColor,
|
|
|
+ strokeOpacity: options.strokeOpacity,
|
|
|
+ strokeWeight: options.strokeWeight,
|
|
|
+ fillColor: options.fillColor,
|
|
|
+ fillOpacity: options.fillOpacity,
|
|
|
strokeStyle: 'solid'
|
|
|
});
|
|
|
overlays.push(rectangle);
|
|
|
map.add(rectangle);
|
|
|
};
|
|
|
- // 创建矩形
|
|
|
+ // 创建多边形
|
|
|
const createPolygon = (options: any) => {
|
|
|
// 将数组转换为AMap.LngLat对象的数组
|
|
|
const path = options.path.map((coord) => {
|
|
@@ -317,23 +287,39 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
});
|
|
|
const polygon = new AMap.Polygon({
|
|
|
path: path,
|
|
|
- strokeColor: options.color,
|
|
|
- strokeOpacity: 1,
|
|
|
- strokeWeight: options.lineWidth,
|
|
|
- fillColor: options.color,
|
|
|
- fillOpacity: options.opacity,
|
|
|
+ strokeColor: options.strokeColor,
|
|
|
+ strokeOpacity: options.strokeOpacity,
|
|
|
+ strokeWeight: options.strokeWeight,
|
|
|
+ fillColor: options.fillColor,
|
|
|
+ fillOpacity: options.fillOpacity,
|
|
|
strokeStyle: 'solid'
|
|
|
});
|
|
|
- overlays.push(polygon);
|
|
|
map.add(polygon);
|
|
|
+ return polygon;
|
|
|
+ };
|
|
|
+ // 创建直线
|
|
|
+ const createStraightLine = (options: any) => {
|
|
|
+ // 将数组转换为AMap.LngLat对象的数组
|
|
|
+ const path = options.path.map((coord) => {
|
|
|
+ return new AMap.LngLat(coord[0], coord[1]);
|
|
|
+ });
|
|
|
+ const polyline = new AMap.Polyline({
|
|
|
+ path: path,
|
|
|
+ strokeColor: options.strokeColor,
|
|
|
+ strokeOpacity: options.strokeOpacity,
|
|
|
+ strokeWeight: options.strokeWeight,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ });
|
|
|
+ overlays.push(polyline);
|
|
|
+ map.add(polyline);
|
|
|
};
|
|
|
// 绘制图标
|
|
|
const createMarker = (options: any) => {
|
|
|
const marker = new AMap.Marker({
|
|
|
- position: new AMap.LngLat(116.38, 39.92),
|
|
|
+ position: options.path,
|
|
|
// 将一张图片的地址设置为 icon
|
|
|
icon: options.icon,
|
|
|
- size: new AMap.Size(options.size[0], options.size[1]),
|
|
|
+ size: [options.size[0], options.size[1]],
|
|
|
anchor: 'bottom-center'
|
|
|
});
|
|
|
map.add(marker);
|
|
@@ -346,11 +332,11 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
});
|
|
|
const polygon = new AMap.Polygon({
|
|
|
path: path,
|
|
|
- strokeColor: options.color,
|
|
|
+ strokeColor: options.strokeColor,
|
|
|
strokeOpacity: 1,
|
|
|
- strokeWeight: options.lineWidth,
|
|
|
- fillColor: options.color,
|
|
|
- fillOpacity: options.opacity,
|
|
|
+ strokeWeight: options.strokeWeight,
|
|
|
+ fillColor: options.fillColor,
|
|
|
+ fillOpacity: options.fillOpacity,
|
|
|
strokeStyle: 'solid'
|
|
|
});
|
|
|
// 计算区域面积
|
|
@@ -360,6 +346,7 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
text: '区域面积' + area + '平方米',
|
|
|
offset: new AMap.Pixel(-20, -20)
|
|
|
});
|
|
|
+ overlays.push(polygon, text);
|
|
|
map.add(polygon);
|
|
|
map.add(text);
|
|
|
};
|
|
@@ -397,20 +384,37 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
};
|
|
|
// 根据索引移除覆盖物
|
|
|
const removeOverlayByIndex = (index: number) => {
|
|
|
- // 移除地图上覆盖物
|
|
|
- map.remove(overlays[index]);
|
|
|
+ if (Array.isArray(overlays[index])) {
|
|
|
+ overlays[index].forEach((overlay) => {
|
|
|
+ // 移除地图上覆盖物
|
|
|
+ map.remove(overlay);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 移除地图上覆盖物
|
|
|
+ map.remove(overlays[index]);
|
|
|
+ }
|
|
|
overlays.splice(index, 1);
|
|
|
overlaysData.splice(index, 1);
|
|
|
};
|
|
|
- // // 图层分析显示信息
|
|
|
- // const addText = (text) => {
|
|
|
- //
|
|
|
- // }
|
|
|
+ const getContextMenu = () => {
|
|
|
+ return contextMenu;
|
|
|
+ };
|
|
|
+ const getAMap = () => {
|
|
|
+ return AMap;
|
|
|
+ };
|
|
|
+ const getMap = () => {
|
|
|
+ return map;
|
|
|
+ };
|
|
|
return {
|
|
|
overlays,
|
|
|
getMouseTool,
|
|
|
+ getAMap,
|
|
|
+ getMap,
|
|
|
initMouseTool,
|
|
|
+ getContextMenu,
|
|
|
+ createGraphics,
|
|
|
drawGraphics,
|
|
|
+ drawGraphics2,
|
|
|
closeDraw,
|
|
|
createCircle,
|
|
|
removeOverlayByIndex,
|