|
@@ -23,6 +23,7 @@ export function useDrawTool() {
|
|
|
const overlays = [];
|
|
|
const overlaysData = [];
|
|
|
let mouseTool, contextMenu, container, map, AMap, rightClickObj;
|
|
|
+ let drawEndMethod;
|
|
|
const initMouseTool = (options2) => {
|
|
|
container = document.getElementById('aMap');
|
|
|
map = options2.map;
|
|
@@ -31,6 +32,9 @@ export function useDrawTool() {
|
|
|
mouseTool = new AMap.MouseTool(map);
|
|
|
//创建右键菜单
|
|
|
contextMenu = new AMap.ContextMenu();
|
|
|
+ if (options2.drawEndMethod) {
|
|
|
+ drawEndMethod = options2.drawEndMethod;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
// 删除图形
|
|
@@ -168,58 +172,69 @@ export function useDrawTool() {
|
|
|
data.id = id;
|
|
|
return { text, data };
|
|
|
};
|
|
|
- let polyline = null;
|
|
|
- let startPoint = null;
|
|
|
- let isDrawing = false;
|
|
|
- let lastPoint = null;
|
|
|
+ let anyLine = null;
|
|
|
+ let drawing = false;
|
|
|
+ let path = null;
|
|
|
// 绘制任意线
|
|
|
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);
|
|
|
- // });
|
|
|
+ map.on('touchstart', handleTouchStart);
|
|
|
+ map.on('touchmove', handleTouchMove);
|
|
|
+ document.addEventListener('touchend', handleTouchEnd);
|
|
|
+ map.on('mousedown', handleTouchStart);
|
|
|
+ map.on('mousemove', handleTouchMove.bind(null, options));
|
|
|
+ document.addEventListener('mouseup', handleTouchEnd.bind(null, options));
|
|
|
};
|
|
|
- // 任意线 鼠标按下事件
|
|
|
- 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 handleTouchStart = (e) => {
|
|
|
+ drawing = true;
|
|
|
+ map.setStatus({
|
|
|
+ showIndoorMap: false,
|
|
|
+ dragEnable: false,
|
|
|
+ keyboardEnable: false,
|
|
|
+ doubleClickZoom: false,
|
|
|
+ zoomEnable: false,
|
|
|
+ rotateEnable: false
|
|
|
+ });
|
|
|
+ path = [e.lnglat];
|
|
|
+ if (anyLine) {
|
|
|
+ map.remove(anyLine);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ const handleTouchMove = (options, e) => {
|
|
|
+ if (!drawing) return;
|
|
|
+ path.push(e.lnglat);
|
|
|
+ if (anyLine) {
|
|
|
+ map.remove(anyLine);
|
|
|
+ }
|
|
|
+ anyLine = new AMap.Polyline({
|
|
|
+ path: path,
|
|
|
+ strokeColor: options.strokeColor,
|
|
|
+ strokeOpacity: options.strokeOpacity,
|
|
|
+ strokeWeight: options.strokeWeight,
|
|
|
+ strokeStyle: options.strokeStyle,
|
|
|
+ lineJoin: 'round'
|
|
|
+ });
|
|
|
+ map.add(anyLine);
|
|
|
+ };
|
|
|
+ const handleTouchEnd = (options) => {
|
|
|
+ drawing = false;
|
|
|
+ map.setStatus({
|
|
|
+ showIndoorMap: true,
|
|
|
+ dragEnable: true,
|
|
|
+ keyboardEnable: true,
|
|
|
+ doubleClickZoom: true,
|
|
|
+ zoomEnable: true,
|
|
|
+ rotateEnable: true
|
|
|
+ });
|
|
|
+ map.off('touchstart', handleTouchStart);
|
|
|
+ map.on('touchmove', handleTouchMove.bind(null, options));
|
|
|
+ document.addEventListener('touchend', handleTouchEnd.bind(null, options));
|
|
|
+ map.off('mousedown', handleTouchStart);
|
|
|
+ map.off('mousemove', handleTouchMove);
|
|
|
+ document.removeEventListener('mouseup', handleTouchEnd);
|
|
|
+ if (!!drawEndMethod) {
|
|
|
+ drawEndMethod(options, anyLine);
|
|
|
+ }
|
|
|
+ anyLine = null;
|
|
|
};
|
|
|
// 关闭绘制
|
|
|
const closeDraw = () => {
|
|
@@ -409,6 +424,9 @@ export function useDrawTool() {
|
|
|
const getMap = () => {
|
|
|
return map;
|
|
|
};
|
|
|
+ const setDrawEndMethod = (newMethod) => {
|
|
|
+ drawEndMethod = newMethod;
|
|
|
+ };
|
|
|
return {
|
|
|
overlays,
|
|
|
getMouseTool,
|
|
@@ -424,6 +442,7 @@ export function useDrawTool() {
|
|
|
removeOverlayByIndex,
|
|
|
handleUndo,
|
|
|
currentState,
|
|
|
- history
|
|
|
+ history,
|
|
|
+ setDrawEndMethod
|
|
|
};
|
|
|
}
|