base-draw.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. import * as ol from 'ol'
  2. import * as style from 'ol/style'
  3. import * as layer from 'ol/layer'
  4. import * as source from 'ol/source'
  5. import * as geom from 'ol/geom'
  6. import * as extent from 'ol/extent'
  7. import * as proj from 'ol/proj'
  8. import * as interaction from 'ol/interaction'
  9. import * as format from "ol/format";
  10. import Modify from "ol/interaction/Modify"
  11. import Draw, {createBox} from "ol/interaction/Draw"
  12. import * as sphere from "ol/sphere";
  13. import {unByKey} from "ol/Observable";
  14. import {formatPosition} from "@/utils/easyMap";
  15. import {isValue} from "@/utils/util";
  16. import {fromCircle} from "ol/geom/Polygon";
  17. import {ElMessage} from "element-plus";
  18. const globalLineDash = [
  19. [0, 0], //实线
  20. [15, 15], //长虚线
  21. [5, 5] //虚线
  22. ]
  23. const layerFlag = ['layerName', 'drawViewsLayer']
  24. const drawFlag = ['interactionName', 'drawInteraction']
  25. const modifyFlag = ['interactionName', 'modifyInteraction']
  26. const baseDrawConfig = {
  27. // 样式字段
  28. text: null, // 要素上显示的文字,默认无文字
  29. pointIcon: null, // Point的图标,默认圆形
  30. pointScale: 1, // Point的缩放,默认1
  31. pointOffset: [0, 0], // Point的偏移量,默认[0, 0]
  32. lineColor: '#2860F1', // LineString的线段颜色,默认蓝色
  33. lineWidth: 1, // LineString的线段宽度,默认1
  34. lineType: 0, // LineString的线段类型索引,默认0,实线,取globalLineDash数组索引
  35. lineDash: null, // LineString的线段类型,默认null,优先级比lineType高
  36. polyColor: 'rgba(20, 129, 241, 0.3)', // Polygon的填充色,默认蓝色
  37. polyBorderColor: '#2860F1', // Polygon的边框颜色,默认蓝色
  38. polyBorderWidth: 1, // Polygon的边框宽度,默认1
  39. polyBorderType: 0, // Polygon的边框类型索引,默认0,实线,取globalLineDash数组索引
  40. polyBorderDash: null, // Polygon的边框类型,默认null,优先级比polyBorderType高
  41. // 业务字段
  42. show: false, // 标绘样式选项是否显示
  43. featureType: 'Point', // 标绘的要素类型
  44. isPoint: false, // 是否可以标绘点
  45. isLineString: false, // 是否可以标绘线
  46. isPolygon: false, // 是否可以标绘面
  47. showPosition: false, // 是否显示经纬度输入框
  48. refreshStyleFunc: () => {}, // 刷新标绘样式方法
  49. }
  50. export const getBaseDrawConfig = () => {
  51. return JSON.parse(JSON.stringify(baseDrawConfig))
  52. }
  53. /**
  54. *
  55. * @param map
  56. * @param arr 要标绘的数组,每个对象的数据在要素中保存的属性为 'val'
  57. * @param arr > wkt:wkt格式坐标
  58. * @param arr > styles:要素的样式,优先级最高
  59. * @param arr > text:styles为空的时候,要素上显示的文字,默认无文字
  60. * @param arr > textOffsetY:styles为空的时候,要素上显示的文字Y轴偏移量,默认Point-30,其他0
  61. * @param arr > pointIcon:styles为空的时候,Point的图标,默认圆形
  62. * @param arr > pointScale:styles为空的时候,Point的缩放,默认1
  63. * @param arr > pointOffset:styles为空的时候,Point的偏移量,默认[0, 0]
  64. * @param arr > lineColor:styles为空的时候,LineString的线段颜色,默认蓝色
  65. * @param arr > lineWidth:styles为空的时候,LineString的线段宽度,默认1
  66. * @param arr > lineType:styles为空的时候,LineString的线段类型索引,默认0,实线,取globalLineDash数组索引
  67. * @param arr > lineDash:styles为空的时候,LineString的线段类型,默认null,优先级比lineType高
  68. * @param arr > polyColor:styles为空的时候,Polygon的填充色,默认蓝色
  69. * @param arr > polyBorderColor:styles为空的时候,Polygon的边框颜色,默认蓝色
  70. * @param arr > polyBorderWidth:styles为空的时候,Polygon的边框宽度,默认1
  71. * @param arr > polyBorderType:styles为空的时候,Polygon的边框类型索引,默认0,实线,取globalLineDash数组索引
  72. * @param arr > polyBorderDash:styles为空的时候,Polygon的边框类型,默认null,优先级比polyBorderType高
  73. */
  74. export const drawViews = (map, arr, isAuto = true) => {
  75. let _source
  76. const realLayer = map.getLayers().getArray().filter(v => v.get(layerFlag[0]) === layerFlag[1])
  77. if (realLayer[0]) {
  78. _source = realLayer[0].getSource()
  79. } else {
  80. _source = new source.Vector(); //图层数据源
  81. const _vector = new layer.Vector({
  82. zIndex: 9999,
  83. source: _source,
  84. });
  85. _vector.set(layerFlag[0], layerFlag[1])
  86. map.addLayer(_vector);
  87. }
  88. _source.clear() // 清空标绘
  89. const autoPosition: any = []
  90. const features = arr.filter(v => {
  91. try {
  92. new format.WKT().readFeature(v.wkt)
  93. return true
  94. } catch (e) {
  95. console.error('错误坐标:' + v.wkt)
  96. return false
  97. }
  98. }).map(v => {
  99. const feat: any = new format.WKT().readFeature(v.wkt)
  100. const type = feat.getGeometry().getType()
  101. switch (type) {
  102. case 'Point': autoPosition.push(feat.getGeometry().getCoordinates())
  103. break
  104. case 'LineString': autoPosition.push(...feat.getGeometry().getCoordinates())
  105. break
  106. case 'Polygon': autoPosition.push(...feat.getGeometry().getCoordinates()[0])
  107. break
  108. }
  109. for (const [kVal, vVal] of Object.entries(v)) {
  110. // @ts-ignore
  111. if (vVal === null || vVal === undefined || (typeof vVal === 'string' && vVal.trim() === '') || (typeof vVal === 'object' && vVal?.length === 0)) {
  112. delete v[kVal]
  113. }
  114. }
  115. const {
  116. textOffsetY = (type === 'Point' ? -30 : 0),
  117. pointIcon, pointScale, pointOffset,
  118. lineColor, lineWidth, lineType, lineDash,
  119. polyColor, polyBorderColor, polyBorderWidth, polyBorderType, polyBorderDash
  120. } = Object.assign(getBaseDrawConfig(), v)
  121. let styles: any = []
  122. if (v.styles) {
  123. styles = v.styles
  124. } else {
  125. if (type === 'Point') {
  126. if (pointIcon) {
  127. styles.push(new style.Style({
  128. image: new style.Icon({
  129. src: pointIcon,
  130. scale: pointScale,
  131. displacement: pointOffset
  132. })
  133. }))
  134. } else {
  135. styles.push(new style.Style({
  136. image: new style.Circle({
  137. radius: 10,
  138. fill: new style.Fill({
  139. color: '#e810dd',
  140. }),
  141. scale: pointScale,
  142. displacement: pointOffset
  143. })
  144. }))
  145. }
  146. } else if (type === 'LineString') {
  147. styles.push(new style.Style({
  148. stroke: new style.Stroke({
  149. color: lineColor,
  150. width: lineWidth,
  151. lineDash: lineDash ?? globalLineDash[Number(lineType)]
  152. })
  153. }))
  154. } else if (type === 'Polygon') {
  155. styles.push(new style.Style({
  156. stroke: new style.Stroke({
  157. color: polyBorderColor,
  158. width: polyBorderWidth,
  159. lineDash: polyBorderDash ?? globalLineDash[Number(polyBorderType)]
  160. }),
  161. fill: new style.Fill({
  162. color: polyColor,
  163. }),
  164. }))
  165. }
  166. if (v.text) {
  167. styles.push(new style.Style({
  168. text: new style.Text({
  169. font: "16px bold 微软雅黑",
  170. text: v.text,
  171. fill: new style.Fill({
  172. color: '#ffffff'
  173. }),
  174. stroke: new style.Stroke({
  175. color: '#D26CDB',
  176. width: 2
  177. }),
  178. offsetY: textOffsetY,
  179. }),
  180. }))
  181. }
  182. }
  183. feat.set('val', v)
  184. feat.setStyle(styles)
  185. return feat
  186. })
  187. _source.addFeatures(features)
  188. if (isAuto) {
  189. getShapeView(map, autoPosition)
  190. }
  191. }
  192. let baseDrawTooltipElement;
  193. let baseDrawHelpTooltipElement;
  194. export const drawEdits = (map, obj, emitWkt, isAuto = true) => {
  195. return new Promise((resolve => {
  196. if (!isValue(obj.textOffsetY)) {
  197. obj.textOffsetY = (obj.featureType === 'Point' ? -30 : 0)
  198. }
  199. let commonStyle = (showText) => {
  200. if (obj.featureType === 'Point') {
  201. return new style.Style({
  202. image: obj.pointIcon ? new style.Icon({
  203. src: obj.pointIcon,
  204. scale: obj.pointScale,
  205. displacement: obj.pointOffset
  206. }) : new style.Circle({
  207. radius: 10,
  208. fill: new style.Fill({
  209. color: '#e810dd',
  210. }),
  211. scale: obj.pointScale,
  212. displacement: obj.pointOffset
  213. }),
  214. text: (showText && obj.text) ? new style.Text({
  215. font: "16px bold 微软雅黑",
  216. text: obj.text,
  217. fill: new style.Fill({
  218. color: '#ffffff'
  219. }),
  220. stroke: new style.Stroke({
  221. color: '#D26CDB',
  222. width: 2
  223. }),
  224. offsetY: obj.textOffsetY,
  225. }) : undefined,
  226. })
  227. } else if (obj.featureType === 'LineString') {
  228. return new style.Style({
  229. stroke: new style.Stroke({
  230. color: obj.lineColor,
  231. width: obj.lineWidth,
  232. lineDash: obj.lineDash ?? globalLineDash[Number(obj.lineType)]
  233. }),
  234. text: (showText && obj.text) ? new style.Text({
  235. font: "16px bold 微软雅黑",
  236. text: obj.text,
  237. fill: new style.Fill({
  238. color: '#ffffff'
  239. }),
  240. stroke: new style.Stroke({
  241. color: '#D26CDB',
  242. width: 2
  243. }),
  244. offsetY: obj.textOffsetY,
  245. }) : undefined,
  246. })
  247. } else if (obj.featureType === 'Polygon') {
  248. return new style.Style({
  249. stroke: new style.Stroke({
  250. color: obj.polyBorderColor,
  251. width: obj.polyBorderWidth,
  252. lineDash: obj.polyBorderDash ?? globalLineDash[Number(obj.polyBorderType)]
  253. }),
  254. fill: new style.Fill({
  255. color: obj.polyColor,
  256. }),
  257. text: (showText && obj.text) ? new style.Text({
  258. font: "16px bold 微软雅黑",
  259. text: obj.text,
  260. fill: new style.Fill({
  261. color: '#ffffff'
  262. }),
  263. stroke: new style.Stroke({
  264. color: '#D26CDB',
  265. width: 2
  266. }),
  267. offsetY: obj.textOffsetY,
  268. }) : undefined,
  269. })
  270. }
  271. }
  272. const getWkt = (feature) => {
  273. const gm = feature.getGeometry()
  274. let wkt = ''
  275. const gType = gm.getType()
  276. if (gType === 'Polygon') {
  277. wkt = formatPosition.cpnTwpn(gm.getCoordinates())
  278. } else if (gType === 'LineString') {
  279. wkt = formatPosition.clTwl(gm.getCoordinates())
  280. } else if (gType === 'Circle') {
  281. const circlePoly = fromCircle(gm, 128)
  282. feature.setGeometry(circlePoly)
  283. wkt = formatPosition.cpnTwpn(circlePoly.getCoordinates())
  284. } else if (gType === 'Point') {
  285. wkt = formatPosition.cptTwpt(gm.getCoordinates())
  286. }
  287. emitWkt(wkt)
  288. }
  289. const reset = () => {
  290. const oldLayer = map.getLayers().getArray().filter(v => v.get(layerFlag[0]) === layerFlag[1])
  291. if (oldLayer) {
  292. map.removeLayer(oldLayer[0])
  293. }
  294. const oldDraw = map.getInteractions().getArray().filter(v => v.get(drawFlag[0]) === drawFlag[1])
  295. if (oldDraw) {
  296. map.removeInteraction(oldDraw[0])
  297. }
  298. const oldModify = map.getInteractions().getArray().filter(v => v.get(modifyFlag[0]) === modifyFlag[1])
  299. if (oldModify) {
  300. map.removeInteraction(oldModify[0])
  301. }
  302. }
  303. if (!baseDrawTooltipElement) {
  304. reset()
  305. let _source
  306. const realLayer = map.getLayers().getArray().filter(v => v.get(layerFlag[0]) === layerFlag[1])
  307. if (realLayer[0]) {
  308. _source = realLayer[0].getSource()
  309. } else {
  310. _source = new source.Vector(); //图层数据源
  311. const _vector = new layer.Vector({
  312. zIndex: 9999,
  313. source: _source,
  314. style: commonStyle(true),
  315. });
  316. _vector.set(layerFlag[0], layerFlag[1])
  317. map.addLayer(_vector);
  318. }
  319. if (obj.wkt) {
  320. try {
  321. const feat: any = new format.WKT().readFeature(obj.wkt)
  322. _source.addFeature(feat)
  323. const autoPosition: any = []
  324. const type = feat.getGeometry().getType()
  325. switch (type) {
  326. case 'Point': autoPosition.push(feat.getGeometry().getCoordinates())
  327. break
  328. case 'LineString': autoPosition.push(...feat.getGeometry().getCoordinates())
  329. break
  330. case 'Polygon': autoPosition.push(...feat.getGeometry().getCoordinates()[0])
  331. break
  332. }
  333. if (isAuto) {
  334. getShapeView(map, autoPosition)
  335. }
  336. } catch (e) {
  337. obj.wkt = ''
  338. ElMessage.warning('坐标格式错误,请重新标绘!')
  339. }
  340. }
  341. const modifyInteraction = new Modify({
  342. source: _source,
  343. });
  344. modifyInteraction.set(modifyFlag[0], modifyFlag[1])
  345. map.addInteraction(modifyInteraction)
  346. modifyInteraction.on('modifyend', evt => {
  347. try {
  348. const feat = evt.features.item(0)
  349. getWkt(feat)
  350. } catch {
  351. }
  352. })
  353. if (!obj.wkt) {
  354. let sketch;
  355. let helpTooltip;
  356. let measureTooltip;
  357. let continueMsg = '双击结束标绘';
  358. const geodesicCheckbox = true;//测地学方式对象
  359. const createMeasureTooltip = () => {
  360. const id = 'baseDrawTooltipElementId'
  361. if (baseDrawTooltipElement) {
  362. map.removeOverlay(map.getOverlayById(id))
  363. baseDrawTooltipElement.parentNode.removeChild(baseDrawTooltipElement);
  364. }
  365. baseDrawTooltipElement = document.createElement('div');
  366. baseDrawTooltipElement.className = 'tooltip tooltip-measure';
  367. measureTooltip = new ol.Overlay({
  368. id,
  369. element: baseDrawTooltipElement,
  370. offset: [0, -15],
  371. positioning: 'bottom-center'
  372. });
  373. map.addOverlay(measureTooltip);
  374. }
  375. const createHelpTooltip = () => {
  376. const id = 'baseDrawHelpTooltipElementId'
  377. if (baseDrawHelpTooltipElement) {
  378. map.removeOverlay(map.getOverlayById(id))
  379. baseDrawHelpTooltipElement.parentNode.removeChild(baseDrawHelpTooltipElement);
  380. }
  381. baseDrawHelpTooltipElement = document.createElement('div');
  382. baseDrawHelpTooltipElement.className = 'tooltip hidden';
  383. helpTooltip = new ol.Overlay({
  384. id,
  385. element: baseDrawHelpTooltipElement,
  386. offset: [15, 0],
  387. positioning: 'center-left'
  388. });
  389. map.addOverlay(helpTooltip);
  390. }
  391. const formatLength = (line) => {
  392. // 获取投影坐标系
  393. const sourceProj = map.getView().getProjection();
  394. // ol/sphere里有getLength()和getArea()用来测量距离和区域面积,默认的投影坐标系是EPSG:3857, 其中有个options的参数,可以设置投影坐标系
  395. const length = sphere.getLength(line, {projection: sourceProj});
  396. // const length = getLength(line);
  397. let output;
  398. if (length > 100) {
  399. const km = Math.round((length / 1000) * 100) / 100;
  400. output = `${km} 千米 <br>${parseFloat(String(km * 0.53995)).toFixed(2)} 海里`;
  401. } else {
  402. output = `${Math.round(length * 100) / 100} m`;
  403. }
  404. return output;
  405. };
  406. //获取圆的面积
  407. const getCircleArea = (circle, projection) => {
  408. const P = 3.14
  409. const radius = getCircleRadius(circle, projection)
  410. return P * radius * radius
  411. }
  412. //获取圆的半径
  413. const getCircleRadius = (circle, projection) => {
  414. return circle.getRadius() * projection.getMetersPerUnit()
  415. }
  416. const formatArea = (polygon, type= 'polygon') => {
  417. let area
  418. const sourceProj = map.getView().getProjection();
  419. // 获取投影坐标系
  420. if (type === 'polygon') {
  421. area = sphere.getArea(polygon, {
  422. projection: sourceProj,
  423. });
  424. } else if (type === 'circle') {
  425. area = getCircleArea(polygon, sourceProj)
  426. }
  427. let output;
  428. if (area > 10000) {
  429. const km = Math.round((area / 1000000) * 100) / 100;
  430. output = `${km} 平方公里<br>${parseFloat(String(km * 0.38610)).toFixed(
  431. 2
  432. )} 平方英里`;
  433. } else {
  434. output = `${Math.round(area * 100) / 100} ` + " m<sup>2</sup>";
  435. }
  436. return output;
  437. };
  438. const addInteraction = () => {
  439. const id = 'baseDrawName'
  440. let drawLastPoint = ''
  441. let drawLastPointTimer: any = null
  442. let drawCircleDBClickTimer: any = null
  443. const draw = new interaction.Draw({
  444. stopClick: true,
  445. condition: (e) => {
  446. // 圆形单击即触发finishCondition,跳过
  447. if (obj.featureType === 'Circle') {
  448. return true
  449. }
  450. const str = e.coordinate.join(',')
  451. let flag = true
  452. // 进行延时判断,避免只要鼠标不移动,单击后间隔很久也会视为双击,
  453. if (!drawLastPointTimer && str === drawLastPoint) {
  454. flag = false
  455. } else {
  456. if (drawLastPointTimer) {
  457. clearTimeout(drawLastPointTimer)
  458. }
  459. drawLastPoint = str
  460. }
  461. drawLastPointTimer = setTimeout(() => {
  462. drawLastPointTimer = null
  463. }, 1000)
  464. return flag
  465. },
  466. finishCondition: (e) => {
  467. if (obj.featureType !== 'Circle') {
  468. return true
  469. }
  470. let flag = true
  471. // 圆形进行双击延时监听判断
  472. if (!drawCircleDBClickTimer) {
  473. flag = false
  474. }
  475. if (drawCircleDBClickTimer) {
  476. clearTimeout(drawCircleDBClickTimer)
  477. }
  478. drawCircleDBClickTimer = setTimeout(() => {
  479. drawCircleDBClickTimer = null
  480. }, 1000)
  481. return flag
  482. },
  483. source: _source,//测量绘制层数据源
  484. type: obj.featureType, //几何图形类型
  485. // geometryFunction: typeSelect === 'rectangle' ? createBox() : null,
  486. style: commonStyle(obj.featureType === 'Point' ? true : false),
  487. });
  488. draw.set('showText', obj.featureType === 'Point' ? true : false)
  489. draw.set(drawFlag[0], drawFlag[1])
  490. draw.set(id, id)
  491. createMeasureTooltip(); //创建测量工具提示框
  492. createHelpTooltip(); //创建帮助提示框
  493. map.addInteraction(draw);
  494. let listener;
  495. //绑定交互绘制工具开始绘制的事件
  496. const drawstartHandle = (evt) => {
  497. sketch = evt.feature; //绘制的要素
  498. let tooltipCoord = evt.coordinate;// 绘制的坐标
  499. //绑定change事件,根据绘制几何类型得到测量长度值或面积值,并将其设置到测量工具提示框中显示
  500. listener = sketch.getGeometry().on('change', function (evt) {
  501. const geom = evt.target
  502. let output;
  503. if (geom.getType() === 'LineString') {
  504. output = formatLength(geom);//长度值
  505. tooltipCoord = geom.getLastCoordinate();//坐标
  506. } else if (geom.getType() === 'Polygon') {
  507. output = formatArea(geom);//面积值
  508. tooltipCoord = geom.getInteriorPoint().getCoordinates();//坐标
  509. } else if (geom.getType() === 'Circle') {
  510. output = formatArea(geom, 'circle');//面积值
  511. tooltipCoord = geom.getCenter()
  512. }
  513. baseDrawTooltipElement.innerHTML = output;//将测量值设置到测量工具提示框中显示
  514. measureTooltip.setPosition(tooltipCoord);//设置测量工具提示框的显示位置
  515. });
  516. }
  517. draw.on('drawstart', drawstartHandle);
  518. //绑定交互绘制工具结束绘制的事件
  519. const copy = (value) => {
  520. const str = document.createElement('input')
  521. str.setAttribute('value', value)
  522. document.body.appendChild(str)
  523. str.select()
  524. document.execCommand('copy')
  525. document.body.removeChild(str)
  526. }
  527. const drawendHandle = (evt) => {
  528. map.removeInteraction(map.getInteractions().getArray().filter(v => v.get(id) === id)[0]);
  529. // 标绘的时候不需要最终结果dom
  530. map.removeOverlay(map.getOverlayById('baseDrawHelpTooltipElementId'))
  531. baseDrawTooltipElement.parentNode.removeChild(baseDrawTooltipElement);
  532. sketch = null; //置空当前绘制的要素对象
  533. baseDrawTooltipElement = null; //置空测量工具提示框对象
  534. baseDrawHelpTooltipElement.parentNode.removeChild(baseDrawHelpTooltipElement);
  535. baseDrawHelpTooltipElement = null; //置空测量工具提示框对象
  536. unByKey(listener);
  537. draw.un('drawstart', drawstartHandle);
  538. draw.un('drawend', drawendHandle);
  539. map.removeInteraction(map.getInteractions().getArray().filter(v => v.get(id) === id)[0]);
  540. map.un('pointermove', pointerMoveHandler)
  541. const baseDrawFeature = evt.feature
  542. getWkt(baseDrawFeature)
  543. }
  544. draw.on('drawend', drawendHandle);
  545. }
  546. addInteraction(); //调用加载绘制交互控件方法,添加绘图进行测量
  547. const pointerMoveHandler = (evt) => {
  548. if (evt.dragging) {
  549. return;
  550. }
  551. let helpMsg = '单击开始标绘';//当前默认提示信息
  552. //判断绘制几何类型设置相应的帮助提示信息
  553. if (sketch) {
  554. const geom = sketch.getGeometry()
  555. helpMsg = continueMsg;
  556. // if (geom.getType() === 'Polygon') {
  557. // helpMsg = continueMsg; //绘制多边形时提示相应内容
  558. // } else if (geom.getType() === 'LineString') {
  559. // helpMsg = continueMsg; //绘制线时提示相应内容
  560. // }
  561. }
  562. if (baseDrawHelpTooltipElement) {
  563. baseDrawHelpTooltipElement.innerHTML = helpMsg; //将提示信息设置到对话框中显示
  564. helpTooltip.setPosition(evt.coordinate);//设置帮助提示框的位置
  565. baseDrawHelpTooltipElement.classList.remove('hidden');//移除帮助提示框的隐藏样式进行显示
  566. }
  567. };
  568. map.on('pointermove', pointerMoveHandler); //地图容器绑定鼠标移动事件,动态显示帮助提示框内容
  569. //地图绑定鼠标移出事件,鼠标移出时为帮助提示框设置隐藏样式
  570. try {
  571. map.getViewport().on('mouseout', () => {
  572. baseDrawHelpTooltipElement.addClass('hidden');
  573. });
  574. } catch (e) {
  575. }
  576. }
  577. }
  578. resolve(() => {
  579. const oldLayer = map.getLayers().getArray().filter(v => v.get(layerFlag[0]) === layerFlag[1])
  580. if (oldLayer) {
  581. oldLayer[0].setStyle(commonStyle(true))
  582. }
  583. // const oldDraw = map.getInteractions().getArray().filter(v => v.get(drawFlag[0]) === drawFlag[1])
  584. // if (oldDraw) {
  585. // oldDraw[0].setStyle(commonStyle(oldDraw[0].get('showText')))
  586. // }
  587. })
  588. }))
  589. }
  590. export const drawExit = (map) => {
  591. if (baseDrawTooltipElement) {
  592. baseDrawTooltipElement.parentNode?.removeChild?.(baseDrawTooltipElement);
  593. baseDrawTooltipElement = null
  594. }
  595. if (baseDrawHelpTooltipElement) {
  596. baseDrawHelpTooltipElement.parentNode?.removeChild?.(baseDrawHelpTooltipElement);
  597. baseDrawHelpTooltipElement = null
  598. }
  599. const oldLayer = map.getLayers().getArray().filter(v => v.get(layerFlag[0]) === layerFlag[1])
  600. if (oldLayer) {
  601. map.removeLayer(oldLayer[0])
  602. }
  603. const oldDraw = map.getInteractions().getArray().filter(v => v.get(drawFlag[0]) === drawFlag[1])
  604. if (oldDraw) {
  605. map.removeInteraction(oldDraw[0])
  606. }
  607. const oldModify = map.getInteractions().getArray().filter(v => v.get(modifyFlag[0]) === modifyFlag[1])
  608. if (oldModify) {
  609. map.removeInteraction(oldModify[0])
  610. }
  611. }
  612. const getShapeView = (map, position, L = 600) => {
  613. const center = extent.getCenter(extent.boundingExtent(position))
  614. let x = 0
  615. let y = 0
  616. position.forEach(v => {
  617. if (Math.abs(v[0] - center[0]) > x) {
  618. x = Math.abs(v[0] - center[0])
  619. }
  620. if (Math.abs(v[1] - center[1]) > y) {
  621. y = Math.abs(v[1] - center[1])
  622. }
  623. })
  624. const resolution = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) / (L / document.body.clientWidth * document.body.clientHeight)
  625. if (map) {
  626. if (position.length > 1) {
  627. map.getView().animate({
  628. center, resolution
  629. })
  630. } else {
  631. map.getView().animate({
  632. center, zoom: 12
  633. })
  634. }
  635. }
  636. return {
  637. center, resolution
  638. }
  639. }