|
@@ -30,8 +30,6 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
AMap = options2.AMap;
|
|
|
// 初始化绘制工具
|
|
|
mouseTool = new AMap.MouseTool(map);
|
|
|
- // 绘制完成事件
|
|
|
- initDrawMethod();
|
|
|
//创建右键菜单
|
|
|
contextMenu = new AMap.ContextMenu();
|
|
|
// 右键删除按钮
|
|
@@ -44,27 +42,27 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
);
|
|
|
};
|
|
|
// 初始化绘制完成回调
|
|
|
- const initDrawMethod = () => {
|
|
|
+ const initDrawMethod = (options) => {
|
|
|
mouseTool.on('draw', function (event) {
|
|
|
const obj = event.obj;
|
|
|
const id = nanoid();
|
|
|
obj._opts.extData = {
|
|
|
id: id
|
|
|
};
|
|
|
- const data: any = {
|
|
|
- id: id,
|
|
|
- type: getType(obj.className),
|
|
|
- color: obj._opts.strokeColor,
|
|
|
- drawType: obj._opts.fillOpacity === 0 ? '1' : '2'
|
|
|
- };
|
|
|
- const path = obj.getPath();
|
|
|
- // 将AMap.LngLat对象数组转换为经纬度数组
|
|
|
- const pathArr = path.map((lngLat) => {
|
|
|
- // 返回经度和纬度的数组
|
|
|
- return [lngLat.lng, lngLat.lat];
|
|
|
- });
|
|
|
- pathArr.push(pathArr[0]);
|
|
|
- data.path = pathArr;
|
|
|
+ const data: any = deepClone(options);
|
|
|
+ data.id = id;
|
|
|
+ if (options.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));
|
|
@@ -93,24 +91,12 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
- // 判断图形类型
|
|
|
- const getType = (type) => {
|
|
|
- let res = '';
|
|
|
- if (type === 'Overlay.Circle') {
|
|
|
- res = 'circle';
|
|
|
- } else if (type === 'Overlay.Rectangle') {
|
|
|
- res = 'rectangle';
|
|
|
- } else if (type === 'Overlay.Polygon') {
|
|
|
- res = 'polygon';
|
|
|
- }
|
|
|
- return res;
|
|
|
- };
|
|
|
- let lnglat = [];
|
|
|
// 绘制图形
|
|
|
const drawGraphics = (newOptions: MouseTool) => {
|
|
|
const data = getRgba(newOptions.color);
|
|
|
if (['circle', 'rectangle', 'polygon'].includes(newOptions.graphicsType)) {
|
|
|
drawOptions = {
|
|
|
+ type: newOptions.graphicsType,
|
|
|
strokeColor: !!data.color ? data.color : newOptions.color,
|
|
|
strokeOpacity: 1,
|
|
|
strokeWeight: newOptions.lineWidth,
|
|
@@ -120,6 +106,7 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
};
|
|
|
} else if (['anyLine', 'straightLine'].includes(newOptions.graphicsType)) {
|
|
|
drawOptions = {
|
|
|
+ type: newOptions.graphicsType,
|
|
|
strokeColor: !!data.color ? data.color : newOptions.color,
|
|
|
strokeOpacity: data.opacity,
|
|
|
strokeWeight: newOptions.lineWidth,
|
|
@@ -127,17 +114,19 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
};
|
|
|
} 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'
|
|
|
+ type: newOptions.graphicsType,
|
|
|
+ anchor: 'bottom-center',
|
|
|
+ markerName: newOptions.markerName,
|
|
|
+ icon: newOptions.icon,
|
|
|
+ size: new AMap.Size(newOptions.size[0], newOptions.size[1])
|
|
|
};
|
|
|
} else if (newOptions.graphicsType === 'text') {
|
|
|
drawOptions = {
|
|
|
+ type: newOptions.graphicsType,
|
|
|
+ text: newOptions.text,
|
|
|
fontSize: newOptions.fontSize,
|
|
|
- color: newOptions.color
|
|
|
+ fontColor: newOptions.fontColor,
|
|
|
+ lnglat: newOptions.lnglat
|
|
|
};
|
|
|
}
|
|
|
closeDraw();
|
|
@@ -155,10 +144,17 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
} else if (newOptions.graphicsType === 'straightLine') {
|
|
|
// 绘制直线
|
|
|
mouseTool.polyline(drawOptions);
|
|
|
+ } else if (newOptions.graphicsType === 'text') {
|
|
|
+ // 绘制文字
|
|
|
+ addText(drawOptions);
|
|
|
} else if (newOptions.graphicsType === 'marker') {
|
|
|
// 绘制图标
|
|
|
mouseTool.marker(drawOptions);
|
|
|
}
|
|
|
+ if (newOptions.graphicsType !== 'text') {
|
|
|
+ // 绘制完成事件
|
|
|
+ initDrawMethod(drawOptions);
|
|
|
+ }
|
|
|
};
|
|
|
const addText = (drawOptions) => {
|
|
|
// 文本覆盖物的样式
|
|
@@ -173,21 +169,29 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
|
|
|
// 创建文本覆盖物
|
|
|
const text = new AMap.Text({
|
|
|
- text: drawOptions.fontText, // 文本内容,可以根据需要自定义
|
|
|
- position: lnglat, // 文本位置(经纬度)
|
|
|
+ text: drawOptions.text, // 文本内容,可以根据需要自定义
|
|
|
+ position: drawOptions.lnglat, // 文本位置(经纬度)
|
|
|
style: textStyle, // 文本样式
|
|
|
zIndex: 100, // 文本层级
|
|
|
draggable: true // 是否可拖动(可选)
|
|
|
- // 如果需要,可以在这里添加点击文本的回调事件
|
|
|
- // events: {
|
|
|
- // click: function() {
|
|
|
- // // 处理文本点击事件
|
|
|
- // alert('您点击了文本!');
|
|
|
- // }
|
|
|
- // }
|
|
|
});
|
|
|
// 将文本覆盖物添加到地图
|
|
|
map.add(text);
|
|
|
+ const id = nanoid();
|
|
|
+ text._opts.extData = {
|
|
|
+ id: id
|
|
|
+ };
|
|
|
+ const data: any = deepClone(drawOptions);
|
|
|
+ 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);
|
|
|
};
|
|
|
let polyline = null;
|
|
|
let startPoint = null;
|
|
@@ -242,21 +246,6 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
isDrawing = true;
|
|
|
lastPoint = startPoint;
|
|
|
};
|
|
|
- // 设置颜色
|
|
|
- const setColor = (color: string) => {
|
|
|
- drawOptions.strokeColor = color;
|
|
|
- drawOptions.fillColor = color;
|
|
|
- };
|
|
|
- // 设置线框面框
|
|
|
- const setDrawType = (drawType: string) => {
|
|
|
- drawOptions.fillOpacity = drawType === '1' ? 0 : 0.5;
|
|
|
- };
|
|
|
- // 设置图形类型
|
|
|
- const setGraphicsType = (type: string) => {
|
|
|
- closeDraw();
|
|
|
- graphicsType = type;
|
|
|
- drawGraphics(type);
|
|
|
- };
|
|
|
// 关闭绘制
|
|
|
const closeDraw = () => {
|
|
|
mouseTool.close();
|
|
@@ -377,9 +366,6 @@ export function useDrawTool(options: DrawToolOptions) {
|
|
|
initMouseTool,
|
|
|
drawGraphics,
|
|
|
closeDraw,
|
|
|
- setColor,
|
|
|
- setDrawType,
|
|
|
- setGraphicsType,
|
|
|
createCircle,
|
|
|
removeOverlayByIndex,
|
|
|
handleUndo,
|