useDrawTool.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import { nanoid } from 'nanoid';
  2. import { useHistory } from '@/hooks/useHistory';
  3. import { deepClone, getRgba } from '@/utils';
  4. export function useDrawTool() {
  5. let drawOptions = {
  6. strokeColor: '#f80102',
  7. strokeOpacity: 1,
  8. strokeWeight: '1',
  9. fillColor: '#f80102',
  10. fillOpacity: 0.5,
  11. strokeStyle: 'solid'
  12. };
  13. const { currentState, commit, undo, history, future } = useHistory();
  14. const overlays = [];
  15. const overlaysData = [];
  16. let mouseTool, contextMenu, map, AMap, rightClickObj;
  17. let drawEndMethod;
  18. const initMouseTool = (options2) => {
  19. map = options2.map;
  20. AMap = options2.AMap;
  21. // 初始化绘制工具
  22. mouseTool = new AMap.MouseTool(map);
  23. //创建右键菜单
  24. contextMenu = new AMap.ContextMenu();
  25. if (options2.drawEndMethod) {
  26. drawEndMethod = options2.drawEndMethod;
  27. }
  28. };
  29. // 删除图形
  30. const deleteGraphics = () => {
  31. const id = rightClickObj.getExtData()?.id;
  32. if (id) {
  33. for (let i = 0; i < overlays.length; i++) {
  34. const overlay = Array.isArray(overlays[i]) ? overlays[i][0] : overlays[i];
  35. if (overlay?.getExtData().id === id) {
  36. removeOverlayByIndex(i);
  37. commit(deepClone(overlaysData));
  38. rightClickObj = null;
  39. }
  40. }
  41. }
  42. };
  43. // 绘制图形
  44. const drawGraphics = (newOptions: MouseTool) => {
  45. const data = getRgba(newOptions.color);
  46. if (['circle', 'rectangle', 'polygon', 'measureArea'].includes(newOptions.graphicsType)) {
  47. drawOptions = {
  48. type: newOptions.graphicsType,
  49. title: newOptions.title,
  50. strokeColor: !!data.color ? data.color : newOptions.color,
  51. strokeOpacity: 1,
  52. strokeWeight: newOptions.lineWidth,
  53. fillColor: data.color,
  54. fillOpacity: data.opacity,
  55. strokeStyle: 'solid'
  56. };
  57. } else if (['anyLine', 'straightLine'].includes(newOptions.graphicsType)) {
  58. drawOptions = {
  59. type: newOptions.graphicsType,
  60. title: newOptions.title,
  61. strokeColor: !!data.color ? data.color : newOptions.color,
  62. strokeOpacity: data.opacity,
  63. strokeWeight: newOptions.lineWidth,
  64. strokeStyle: 'solid'
  65. };
  66. } else if (newOptions.graphicsType === 'marker') {
  67. drawOptions = {
  68. type: newOptions.graphicsType,
  69. anchor: 'bottom-center',
  70. title: newOptions.title,
  71. icon: newOptions.icon,
  72. iconName: newOptions.iconName,
  73. size: [newOptions.size[0], newOptions.size[1]]
  74. };
  75. } else if (newOptions.graphicsType === 'text') {
  76. drawOptions = {
  77. type: newOptions.graphicsType,
  78. title: newOptions.title,
  79. text: newOptions.text,
  80. fontSize: newOptions.fontSize,
  81. fontColor: newOptions.fontColor,
  82. lnglat: newOptions.lnglat
  83. };
  84. }
  85. closeDraw();
  86. if (newOptions.graphicsType === 'circle') {
  87. // 绘制圆形
  88. mouseTool.circle(drawOptions);
  89. } else if (newOptions.graphicsType === 'rectangle') {
  90. // 绘制矩形
  91. mouseTool.rectangle(drawOptions);
  92. } else if (newOptions.graphicsType === 'polygon') {
  93. // 绘制多边形
  94. mouseTool.polygon(drawOptions);
  95. } else if (newOptions.graphicsType === 'anyLine') {
  96. drawAnyLine(drawOptions);
  97. } else if (newOptions.graphicsType === 'straightLine') {
  98. // 绘制直线
  99. mouseTool.polyline(drawOptions);
  100. } else if (newOptions.graphicsType === 'text') {
  101. // 绘制文字
  102. return addText(drawOptions);
  103. } else if (newOptions.graphicsType === 'marker') {
  104. // 绘制图标
  105. mouseTool.marker(drawOptions);
  106. } else if (newOptions.graphicsType === 'measureArea') {
  107. // 测量面积
  108. mouseTool.polygon(drawOptions);
  109. }
  110. return drawOptions;
  111. };
  112. // 空间分析绘制图形
  113. const drawGraphics2 = (newOptions: MouseTool) => {
  114. drawOptions = {
  115. type: newOptions.graphicsType,
  116. strokeColor: newOptions.color,
  117. strokeOpacity: 1,
  118. strokeWeight: '1',
  119. fillColor: newOptions.color,
  120. fillOpacity: newOptions.drawType === '1' ? 0 : 0.5,
  121. strokeStyle: 'solid'
  122. };
  123. closeDraw();
  124. if (newOptions.graphicsType === 'circle') {
  125. // 绘制圆形
  126. mouseTool.circle(drawOptions);
  127. } else if (newOptions.graphicsType === 'rectangle') {
  128. // 绘制矩形
  129. mouseTool.rectangle(drawOptions);
  130. } else if (newOptions.graphicsType === 'polygon') {
  131. // 绘制多边形
  132. mouseTool.polygon(drawOptions);
  133. }
  134. };
  135. const addText = (options) => {
  136. // 文本覆盖物的样式
  137. const textStyle = {
  138. fontSize: options.fontSize,
  139. color: options.fontColor,
  140. borderColor: 'transparent',
  141. backgroundColor: 'transparent',
  142. borderWidth: 0,
  143. cursor: 'pointer' // 鼠标悬停时显示指针
  144. };
  145. // 创建文本覆盖物
  146. const text = new AMap.Text({
  147. text: options.text, // 文本内容,可以根据需要自定义
  148. position: options.lnglat, // 文本位置(经纬度)
  149. style: textStyle, // 文本样式
  150. zIndex: 100, // 文本层级
  151. draggable: false // 是否可拖动(可选)
  152. });
  153. // 将文本覆盖物添加到地图
  154. map.add(text);
  155. const id = nanoid();
  156. text._opts.extData = {
  157. id: id
  158. };
  159. const data: any = deepClone(options);
  160. data.id = id;
  161. return { text, data };
  162. };
  163. let anyLine = null;
  164. let drawing = false;
  165. let path = null;
  166. // 绘制任意线
  167. const drawAnyLine = (options) => {
  168. map.on('touchstart', handleTouchStart);
  169. map.on('touchmove', handleTouchMove);
  170. document.addEventListener('touchend', handleTouchEnd);
  171. map.on('mousedown', handleTouchStart);
  172. map.on('mousemove', handleTouchMove.bind(null, options));
  173. document.addEventListener('mouseup', handleTouchEnd.bind(null, options));
  174. };
  175. const handleTouchStart = (e) => {
  176. drawing = true;
  177. map.setStatus({
  178. showIndoorMap: false,
  179. dragEnable: false,
  180. keyboardEnable: false,
  181. doubleClickZoom: false,
  182. zoomEnable: false,
  183. rotateEnable: false
  184. });
  185. path = [e.lnglat];
  186. if (anyLine) {
  187. map.remove(anyLine);
  188. }
  189. };
  190. const handleTouchMove = (options, e) => {
  191. if (!drawing) return;
  192. path.push(e.lnglat);
  193. if (anyLine) {
  194. map.remove(anyLine);
  195. }
  196. anyLine = new AMap.Polyline({
  197. path: path,
  198. strokeColor: options.strokeColor,
  199. strokeOpacity: options.strokeOpacity,
  200. strokeWeight: options.strokeWeight,
  201. strokeStyle: options.strokeStyle,
  202. lineJoin: 'round'
  203. });
  204. map.add(anyLine);
  205. };
  206. const handleTouchEnd = (options) => {
  207. drawing = false;
  208. map.setStatus({
  209. showIndoorMap: true,
  210. dragEnable: true,
  211. keyboardEnable: true,
  212. doubleClickZoom: true,
  213. zoomEnable: true,
  214. rotateEnable: true
  215. });
  216. map.off('touchstart', handleTouchStart);
  217. map.on('touchmove', handleTouchMove.bind(null, options));
  218. document.addEventListener('touchend', handleTouchEnd.bind(null, options));
  219. map.off('mousedown', handleTouchStart);
  220. map.off('mousemove', handleTouchMove);
  221. document.removeEventListener('mouseup', handleTouchEnd);
  222. if (!!drawEndMethod) {
  223. drawEndMethod(options, anyLine);
  224. }
  225. anyLine = null;
  226. };
  227. // 关闭绘制
  228. const closeDraw = () => {
  229. mouseTool.close();
  230. };
  231. // 返回鼠标工具对象
  232. const getMouseTool = () => {
  233. return mouseTool;
  234. };
  235. // 创建图形
  236. const createGraphics = (data: any) => {
  237. if (data.type === 'circle') {
  238. // 绘制圆形
  239. return createCircle(data);
  240. } else if (['polygon', 'rectangle'].includes(data.type)) {
  241. // 绘制矩形、多边形
  242. return createPolygon(data);
  243. } else if (data.type === 'marker') {
  244. // 绘制图标
  245. return createMarker(data);
  246. } else if (data.type === 'measureArea') {
  247. // 绘制面积
  248. return createMeasureArea(data);
  249. } else if (data.type === 'text') {
  250. // 文字
  251. return addText(data);
  252. } else if (data.type === 'straightLine') {
  253. // 直线
  254. return createStraightLine(data);
  255. }
  256. };
  257. // 创建圆形
  258. const createCircle = (options) => {
  259. const circle = new AMap.Circle({
  260. center: options.center,
  261. radius: options.radius, //半径
  262. strokeColor: options.strokeColor,
  263. strokeOpacity: options.strokeOpacity,
  264. strokeWeight: options.strokeWeight,
  265. fillColor: options.fillColor,
  266. fillOpacity: options.fillOpacity,
  267. strokeStyle: 'solid'
  268. });
  269. map.add(circle);
  270. return circle;
  271. };
  272. // 创建矩形
  273. const createRectangle = (options: any) => {
  274. const southWest = new AMap.LngLat(options.southWest[0], options.southWest[1]);
  275. const northEast = new AMap.LngLat(options.northEast[0], options.northEast[1]);
  276. const bounds = new AMap.Bounds(southWest, northEast);
  277. const rectangle = new AMap.Rectangle({
  278. bounds: bounds,
  279. strokeColor: options.strokeColor,
  280. strokeOpacity: options.strokeOpacity,
  281. strokeWeight: options.strokeWeight,
  282. fillColor: options.fillColor,
  283. fillOpacity: options.fillOpacity,
  284. strokeStyle: 'solid'
  285. });
  286. overlays.push(rectangle);
  287. map.add(rectangle);
  288. };
  289. // 创建多边形
  290. const createPolygon = (options: any) => {
  291. // 将数组转换为AMap.LngLat对象的数组
  292. const path = options.path.map((coord) => {
  293. return new AMap.LngLat(coord[0], coord[1]);
  294. });
  295. const polygon = new AMap.Polygon({
  296. path: path,
  297. strokeColor: options.strokeColor,
  298. strokeOpacity: options.strokeOpacity,
  299. strokeWeight: options.strokeWeight,
  300. fillColor: options.fillColor,
  301. fillOpacity: options.fillOpacity,
  302. strokeStyle: 'solid'
  303. });
  304. map.add(polygon);
  305. return polygon;
  306. };
  307. // 创建直线
  308. const createStraightLine = (options: any) => {
  309. // 将数组转换为AMap.LngLat对象的数组
  310. const path = options.path.map((coord) => {
  311. return new AMap.LngLat(coord[0], coord[1]);
  312. });
  313. const polyline = new AMap.Polyline({
  314. path: path,
  315. strokeColor: options.strokeColor,
  316. strokeOpacity: options.strokeOpacity,
  317. strokeWeight: options.strokeWeight,
  318. strokeStyle: 'solid'
  319. });
  320. overlays.push(polyline);
  321. map.add(polyline);
  322. };
  323. // 绘制图标
  324. const createMarker = (options: any) => {
  325. const marker = new AMap.Marker({
  326. position: options.path,
  327. // 将一张图片的地址设置为 icon
  328. icon: options.icon,
  329. size: [options.size[0], options.size[1]],
  330. anchor: 'bottom-center'
  331. });
  332. map.add(marker);
  333. };
  334. // 绘制测量面积
  335. const createMeasureArea = (options: any) => {
  336. // 将数组转换为AMap.LngLat对象的数组
  337. const path = options.path.map((coord) => {
  338. return new AMap.LngLat(coord[0], coord[1]);
  339. });
  340. const polygon = new AMap.Polygon({
  341. path: path,
  342. strokeColor: options.strokeColor,
  343. strokeOpacity: 1,
  344. strokeWeight: options.strokeWeight,
  345. fillColor: options.fillColor,
  346. fillOpacity: options.fillOpacity,
  347. strokeStyle: 'solid'
  348. });
  349. // 计算区域面积
  350. const area = Math.round(AMap.GeometryUtil.ringArea(options.path));
  351. const text = new AMap.Text({
  352. position: path[path.length - 1],
  353. text: '区域面积' + area + '平方米',
  354. offset: new AMap.Pixel(-20, -20)
  355. });
  356. overlays.push(polygon, text);
  357. map.add(polygon);
  358. map.add(text);
  359. };
  360. // 处理撤销
  361. const handleUndo = () => {
  362. const previous = history.value[history.value.length - 2];
  363. if (history.value.length > 1) {
  364. if (currentState.value.length > previous.length) {
  365. // 撤销新增
  366. removeOverlayByIndex(currentState.value.length - 1);
  367. } else {
  368. let restoreData;
  369. for (let i = 0; i < previous.length; i++) {
  370. let index = 0;
  371. for (let k = 0; k < currentState.value.length; k++) {
  372. if (previous[i].id !== currentState.value[k].id) {
  373. index++;
  374. } else {
  375. break;
  376. }
  377. }
  378. if (index === previous.length - 1) {
  379. restoreData = previous[i];
  380. break;
  381. }
  382. }
  383. if (restoreData) {
  384. createGraphics(restoreData);
  385. }
  386. }
  387. undo();
  388. console.log(history.value, future.value, currentState.value);
  389. }
  390. };
  391. // 根据索引移除覆盖物
  392. const removeOverlayByIndex = (index: number) => {
  393. if (Array.isArray(overlays[index])) {
  394. overlays[index].forEach((overlay) => {
  395. // 移除地图上覆盖物
  396. map.remove(overlay);
  397. });
  398. } else {
  399. // 移除地图上覆盖物
  400. map.remove(overlays[index]);
  401. }
  402. overlays.splice(index, 1);
  403. overlaysData.splice(index, 1);
  404. };
  405. const getContextMenu = () => {
  406. return contextMenu;
  407. };
  408. const getAMap = () => {
  409. return AMap;
  410. };
  411. const getMap = () => {
  412. return map;
  413. };
  414. const setDrawEndMethod = (newMethod) => {
  415. drawEndMethod = newMethod;
  416. };
  417. return {
  418. overlays,
  419. getMouseTool,
  420. getAMap,
  421. getMap,
  422. initMouseTool,
  423. getContextMenu,
  424. createGraphics,
  425. drawGraphics,
  426. drawGraphics2,
  427. closeDraw,
  428. createCircle,
  429. removeOverlayByIndex,
  430. handleUndo,
  431. currentState,
  432. history,
  433. setDrawEndMethod
  434. };
  435. }