|
@@ -1,6 +1,7 @@
|
|
|
import { nanoid } from 'nanoid';
|
|
|
import { useHistory } from '@/hooks/useHistory';
|
|
|
-import { deepClone } from "@/utils";
|
|
|
+import { deepClone } from '@/utils';
|
|
|
+import { countCircleArea, countRectangleArea } from '@/utils/geometryUtil';
|
|
|
|
|
|
interface DrawToolOptions {
|
|
|
color: string;
|
|
@@ -57,13 +58,56 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
if (data.type === 'circle') {
|
|
|
data.center = [obj.getCenter().lng, obj.getCenter().lat];
|
|
|
data.radius = obj.getRadius();
|
|
|
- }
|
|
|
- if (typeof options.onDrawCompleted === 'function') {
|
|
|
- options.onDrawCompleted(data, event);
|
|
|
+ const area = countCircleArea(data.center, data.radius);
|
|
|
+ // 计算区域面积
|
|
|
+ const text = new AMap.Text({
|
|
|
+ position: new AMap.LngLat(data.center[0], data.center[1]),
|
|
|
+ text: '区域面积' + area + '平方米',
|
|
|
+ offset: new AMap.Pixel(-20, -20)
|
|
|
+ });
|
|
|
+ map.add(text);
|
|
|
+ } else if (data.type === 'rectangle') {
|
|
|
+ const bounds = obj.getBounds();
|
|
|
+ // 从bounds中获取西南角和东北角的坐标
|
|
|
+ const southWest = bounds.getSouthWest();
|
|
|
+ const northEast = bounds.getNorthEast();
|
|
|
+ data.southWest = [southWest.lng, southWest.lat];
|
|
|
+ data.northEast = [northEast.lng, northEast.lat];
|
|
|
+ const pathArr = [
|
|
|
+ [
|
|
|
+ [southWest.lng, northEast.lat],
|
|
|
+ [northEast.lng, northEast.lat],
|
|
|
+ [northEast.lng, southWest.lat],
|
|
|
+ [southWest.lng, southWest.lat],
|
|
|
+ [southWest.lng, northEast.lat]
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ const area = countRectangleArea(pathArr);
|
|
|
+ // 计算区域面积
|
|
|
+ const text = new AMap.Text({
|
|
|
+ position: new AMap.LngLat(southWest.lng, northEast.lat),
|
|
|
+ text: '区域面积' + area + '平方米',
|
|
|
+ offset: new AMap.Pixel(-20, -20)
|
|
|
+ });
|
|
|
+ map.add(text);
|
|
|
+ } else if (data.type === 'polygon') {
|
|
|
+ const path = obj.getPath();
|
|
|
+ // 将AMap.LngLat对象数组转换为经纬度数组
|
|
|
+ const pathArr = path.map((lngLat) => {
|
|
|
+ // 返回经度和纬度的数组
|
|
|
+ return [lngLat.lng, lngLat.lat];
|
|
|
+ });
|
|
|
+ data.path = pathArr;
|
|
|
+ const newPathArr = deepClone(pathArr);
|
|
|
+ newPathArr.push(newPathArr[0]);
|
|
|
+ const area = countRectangleArea([newPathArr]);
|
|
|
}
|
|
|
overlays.push(obj);
|
|
|
overlaysData.push(data);
|
|
|
commit(deepClone(overlaysData));
|
|
|
+ if (typeof options.onDrawCompleted === 'function') {
|
|
|
+ options.onDrawCompleted(data, overlaysData, event);
|
|
|
+ }
|
|
|
// 右击进入编辑
|
|
|
obj.on('rightclick', handleRightClick);
|
|
|
});
|
|
@@ -141,10 +185,10 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
createCircle(data);
|
|
|
} else if (data.type === 'rectangle') {
|
|
|
// 绘制矩形
|
|
|
- mouseTool.rectangle(drawOptions);
|
|
|
+ createRectangle(data);
|
|
|
} else if (data.type === 'polygon') {
|
|
|
// 绘制多边形
|
|
|
- mouseTool.polygon(drawOptions);
|
|
|
+ createPolygon(data);
|
|
|
}
|
|
|
};
|
|
|
// 创建圆形
|
|
@@ -164,36 +208,70 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
};
|
|
|
// 创建矩形
|
|
|
const createRectangle = (options: any) => {
|
|
|
-
|
|
|
+ const southWest = new AMap.LngLat(options.southWest[0], options.southWest[1]);
|
|
|
+ const northEast = new AMap.LngLat(options.northEast[0], options.northEast[1]);
|
|
|
+ const bounds = new AMap.Bounds(southWest, northEast);
|
|
|
+ const rectangle = new AMap.Rectangle({
|
|
|
+ bounds: bounds,
|
|
|
+ strokeColor: options.color,
|
|
|
+ strokeOpacity: 1,
|
|
|
+ strokeWeight: 2,
|
|
|
+ fillColor: options.color,
|
|
|
+ fillOpacity: options.drawType === '1' ? 0 : 0.5,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ });
|
|
|
+ overlays.push(rectangle);
|
|
|
+ map.add(rectangle);
|
|
|
+ };
|
|
|
+ // 创建矩形
|
|
|
+ const createPolygon = (options: any) => {
|
|
|
+ // 将数组转换为AMap.LngLat对象的数组
|
|
|
+ const path = options.path.map((coord) => {
|
|
|
+ return new AMap.LngLat(coord[0], coord[1]);
|
|
|
+ });
|
|
|
+ const polygon = new AMap.Polygon({
|
|
|
+ path: path,
|
|
|
+ strokeColor: options.color,
|
|
|
+ strokeOpacity: 1,
|
|
|
+ strokeWeight: 2,
|
|
|
+ fillColor: options.color,
|
|
|
+ fillOpacity: options.drawType === '1' ? 0 : 0.5,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ });
|
|
|
+ overlays.push(polygon);
|
|
|
+ map.add(polygon);
|
|
|
};
|
|
|
// 处理撤销
|
|
|
const handleUndo = () => {
|
|
|
- const len = currentState.value.length;
|
|
|
- undo();
|
|
|
- if (len > currentState.value.length) {
|
|
|
- // 撤销新增
|
|
|
- removeOverlayByIndex(currentState.value.length);
|
|
|
- } else {
|
|
|
- let restoreData;
|
|
|
- for (let i = 0; i < currentState.value.length; i++) {
|
|
|
- let index = 0;
|
|
|
- for (let k = 0; k < overlays.length; k++) {
|
|
|
- if (currentState.value[i].id !== overlays[k].getExtData()?.id) {
|
|
|
- index++;
|
|
|
- } else {
|
|
|
+ const previous = history.value[history.value.length - 2];
|
|
|
+ if (history.value.length > 1) {
|
|
|
+ if (currentState.value.length > previous.length) {
|
|
|
+ // 撤销新增
|
|
|
+ removeOverlayByIndex(currentState.value.length - 1);
|
|
|
+ } else {
|
|
|
+ let restoreData;
|
|
|
+ for (let i = 0; i < previous.length; i++) {
|
|
|
+ let index = 0;
|
|
|
+ for (let k = 0; k < currentState.value.length; k++) {
|
|
|
+ if (previous[i].id !== currentState.value[k].id) {
|
|
|
+ index++;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (index === previous.length - 1) {
|
|
|
+ restoreData = previous[i];
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if (index === currentState.value.length - 1) {
|
|
|
- restoreData = currentState.value[i];
|
|
|
- break;
|
|
|
+ if (restoreData) {
|
|
|
+ createGraphics(restoreData);
|
|
|
}
|
|
|
}
|
|
|
- if (restoreData) {
|
|
|
- createGraphics(restoreData);
|
|
|
- }
|
|
|
+ undo();
|
|
|
+
|
|
|
+ console.log(history.value, future.value, currentState.value);
|
|
|
}
|
|
|
- console.log(history.value, future.value, currentState.value);
|
|
|
};
|
|
|
// 根据索引移除覆盖物
|
|
|
const removeOverlayByIndex = (index: number) => {
|
|
@@ -202,6 +280,10 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
overlays.splice(index, 1);
|
|
|
overlaysData.splice(index, 1);
|
|
|
};
|
|
|
+ // // 图层分析显示信息
|
|
|
+ // const addText = (text) => {
|
|
|
+ //
|
|
|
+ // }
|
|
|
return {
|
|
|
overlays,
|
|
|
getMouseTool,
|