|
@@ -1,6 +1,6 @@
|
|
|
import { nanoid } from 'nanoid';
|
|
|
import { useHistory } from '@/hooks/useHistory';
|
|
|
-import { deepClone } from '@/utils';
|
|
|
+import { deepClone, getRgba } from '@/utils';
|
|
|
import { countCircleArea, countRectangleArea } from '@/utils/geometryUtil';
|
|
|
|
|
|
interface DrawToolOptions {
|
|
@@ -11,22 +11,23 @@ interface DrawToolOptions {
|
|
|
onDrawCompleted?: Function;
|
|
|
}
|
|
|
export function useDrawTool(options: DrawToolOptions) {
|
|
|
- const drawOptions = {
|
|
|
- strokeColor: options.color,
|
|
|
+ let drawOptions = {
|
|
|
+ strokeColor: '#f80102',
|
|
|
strokeOpacity: 1,
|
|
|
- strokeWeight: options.lineWidth,
|
|
|
- fillColor: options.color,
|
|
|
- fillOpacity: options.drawType === '1' ? 0 : 0.5,
|
|
|
+ strokeWeight: '1',
|
|
|
+ fillColor: '#f80102',
|
|
|
+ fillOpacity: 0.5,
|
|
|
strokeStyle: 'solid'
|
|
|
};
|
|
|
const { currentState, commit, undo, history, future } = useHistory();
|
|
|
const overlays = [];
|
|
|
const overlaysData = [];
|
|
|
let graphicsType = options.graphicsType;
|
|
|
- let mouseTool, contextMenu, map, AMap, rightClickObj;
|
|
|
- const initMouseTool = (options) => {
|
|
|
- map = options.map;
|
|
|
- AMap = options.AMap;
|
|
|
+ let mouseTool, contextMenu, container, map, AMap, rightClickObj;
|
|
|
+ const initMouseTool = (options2) => {
|
|
|
+ container = document.getElementById('aMap');
|
|
|
+ map = options2.map;
|
|
|
+ AMap = options2.AMap;
|
|
|
// 初始化绘制工具
|
|
|
mouseTool = new AMap.MouseTool(map);
|
|
|
// 绘制完成事件
|
|
@@ -104,20 +105,143 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
}
|
|
|
return res;
|
|
|
};
|
|
|
+ let lnglat = [];
|
|
|
// 绘制图形
|
|
|
const drawGraphics = (newOptions: MouseTool) => {
|
|
|
- options = deepClone(newOptions);
|
|
|
- if (options.graphicsType === 'circle') {
|
|
|
+ const data = getRgba(newOptions.color);
|
|
|
+ if (['circle', 'rectangle', 'polygon'].includes(newOptions.graphicsType)) {
|
|
|
+ drawOptions = {
|
|
|
+ strokeColor: !!data.color ? data.color : newOptions.color,
|
|
|
+ strokeOpacity: 1,
|
|
|
+ strokeWeight: newOptions.lineWidth,
|
|
|
+ fillColor: data.color,
|
|
|
+ fillOpacity: data.opacity,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ };
|
|
|
+ } else if (['anyLine', 'straightLine'].includes(newOptions.graphicsType)) {
|
|
|
+ drawOptions = {
|
|
|
+ strokeColor: !!data.color ? data.color : newOptions.color,
|
|
|
+ strokeOpacity: data.opacity,
|
|
|
+ strokeWeight: newOptions.lineWidth,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ };
|
|
|
+ } else if (newOptions.graphicsType === 'marker') {
|
|
|
+ drawOptions = {
|
|
|
+ strokeColor: !!data.color ? data.color : newOptions.color,
|
|
|
+ strokeOpacity: 1,
|
|
|
+ strokeWeight: newOptions.lineWidth,
|
|
|
+ fillColor: data.color,
|
|
|
+ fillOpacity: data.opacity,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ };
|
|
|
+ } else if (newOptions.graphicsType === 'text') {
|
|
|
+ drawOptions = {
|
|
|
+ fontSize: newOptions.fontSize,
|
|
|
+ color: newOptions.color
|
|
|
+ };
|
|
|
+ }
|
|
|
+ closeDraw();
|
|
|
+ if (newOptions.graphicsType === 'circle') {
|
|
|
// 绘制圆形
|
|
|
mouseTool.circle(drawOptions);
|
|
|
- } else if (options.graphicsType === 'rectangle') {
|
|
|
+ } else if (newOptions.graphicsType === 'rectangle') {
|
|
|
// 绘制矩形
|
|
|
mouseTool.rectangle(drawOptions);
|
|
|
- } else if (options.graphicsType === 'polygon') {
|
|
|
+ } else if (newOptions.graphicsType === 'polygon') {
|
|
|
// 绘制多边形
|
|
|
mouseTool.polygon(drawOptions);
|
|
|
+ } else if (newOptions.graphicsType === 'anyLine') {
|
|
|
+ drawAnyLine(drawOptions);
|
|
|
+ } else if (newOptions.graphicsType === 'straightLine') {
|
|
|
+ // 绘制直线
|
|
|
+ mouseTool.polyline(drawOptions);
|
|
|
+ } else if (newOptions.graphicsType === 'marker') {
|
|
|
+ // 绘制图标
|
|
|
+ mouseTool.marker(drawOptions);
|
|
|
}
|
|
|
};
|
|
|
+ const addText = (drawOptions) => {
|
|
|
+ // 文本覆盖物的样式
|
|
|
+ const textStyle = {
|
|
|
+ fontSize: drawOptions.fontSize,
|
|
|
+ color: drawOptions.fontColor,
|
|
|
+ borderColor: 'transparent',
|
|
|
+ backgroundColor: 'transparent',
|
|
|
+ borderWidth: 0,
|
|
|
+ cursor: 'pointer' // 鼠标悬停时显示指针
|
|
|
+ };
|
|
|
+
|
|
|
+ // 创建文本覆盖物
|
|
|
+ const text = new AMap.Text({
|
|
|
+ text: drawOptions.fontText, // 文本内容,可以根据需要自定义
|
|
|
+ position: lnglat, // 文本位置(经纬度)
|
|
|
+ style: textStyle, // 文本样式
|
|
|
+ zIndex: 100, // 文本层级
|
|
|
+ draggable: true // 是否可拖动(可选)
|
|
|
+ // 如果需要,可以在这里添加点击文本的回调事件
|
|
|
+ // events: {
|
|
|
+ // click: function() {
|
|
|
+ // // 处理文本点击事件
|
|
|
+ // alert('您点击了文本!');
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ });
|
|
|
+ // 将文本覆盖物添加到地图
|
|
|
+ map.add(text);
|
|
|
+ };
|
|
|
+ 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 drawAnyLineMouseDown = (e) => {
|
|
|
+ // 阻止默认行为
|
|
|
+ e.preventDefault();
|
|
|
+ // 获取鼠标在地图上的位置
|
|
|
+ const mapPoint = map.getContainer().getBoundingClientRect();
|
|
|
+ const lngLat = map.getLngLatFromContainerPixel([e.clientX - mapPoint.left, e.clientY - mapPoint.top]);
|
|
|
+
|
|
|
+ startPoint = lngLat;
|
|
|
+ isDrawing = true;
|
|
|
+ lastPoint = startPoint;
|
|
|
+ };
|
|
|
// 设置颜色
|
|
|
const setColor = (color: string) => {
|
|
|
drawOptions.strokeColor = color;
|