leaflet.draw.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. let mousemoveStatus = true;
  2. // del-刘梦祥-时间(删除点中的角度) "<br/>" + pointAngle + "度"
  3. L.Control.Draw = L.Control.extend({
  4. //是否初始化
  5. _initialized: false,
  6. //统计
  7. _lC: 0,
  8. //测量对象集合
  9. _measureObjs: {},
  10. //是否完成当前测绘
  11. _finished: true,
  12. //新测量
  13. isNewElevation: true,
  14. moveMarker: false,
  15. //测量参数
  16. options: {
  17. position: "topright",
  18. autoZIndex: true,
  19. offset: [10, 10],
  20. background: "#000",
  21. color: "#fff",
  22. size: 14,
  23. closeButton: true,
  24. iconUrl: "./images/marker-icon.png",
  25. },
  26. initialize: function (options) {
  27. L.setOptions(this, options);
  28. return this;
  29. },
  30. onAdd: function (map) {
  31. this._map = map;
  32. this._createControl();
  33. switch (this.options.position) {
  34. case "topleft":
  35. this._container.style.marginLeft = this.options.offset[0] + "px";
  36. this._container.style.marginTop = this.options.offset[1] + "px";
  37. break;
  38. case "topright":
  39. this._container.style.marginRight = this.options.offset[0] + "px";
  40. this._container.style.marginTop = this.options.offset[1] + "px";
  41. break;
  42. case "bottomleft":
  43. this._container.style.marginLeft = this.options.offset[0] + "px";
  44. this._container.style.marginBottom = this.options.offset[1] + "px";
  45. break;
  46. case "bottomright":
  47. this._container.style.marginRight = this.options.offset[0] + "px";
  48. this._container.style.marginBottom = this.options.offset[1] + "px";
  49. break;
  50. }
  51. return this._container;
  52. },
  53. _createControl: function () {
  54. var _this = this;
  55. this._container = L.DomUtil.create(
  56. "div",
  57. "leaflet-bar leaflet-control-measure"
  58. );
  59. var link = L.DomUtil.create(
  60. "a",
  61. "leaflet-control-measure-link",
  62. this._container
  63. );
  64. link.title = "测量";
  65. L.DomUtil.create("span", "", link);
  66. L.DomEvent.on(this._container, "contextmenu", L.DomEvent.stopPropagation)
  67. .on(link, "click", L.DomEvent.stopPropagation)
  68. .on(link, "click", function () {
  69. if (_this._finished) {
  70. //开启新的测量
  71. _this.start();
  72. } else {
  73. _this._finished = true;
  74. }
  75. });
  76. // _this.start();
  77. },
  78. start: function () {
  79. var _this = this;
  80. _this._finished = false;
  81. L.DomUtil.addClass(_this._container, "active");
  82. _this._addMeasureGroup();
  83. },
  84. _addMeasureGroup: function () {
  85. var _this = this;
  86. if (!_this._initialized) {
  87. _this._measureGroup = new L.featureGroup();
  88. _this._measureGroup.addTo(this._map);
  89. _this._initialized = true;
  90. }
  91. if (_this.isNewElevation) {
  92. _this._lC++;
  93. _this._measureObjs[_this._lC] = new L.FeatureGroup();
  94. _this._measureObjs[_this._lC].addTo(_this._measureGroup);
  95. _this.isNewElevation = false;
  96. _this._measureObjs[_this._lC].measurePoints = [];
  97. }
  98. _this.tmpMarkers = [];
  99. //关闭地图双击事件
  100. map2DViewer.map.doubleClickZoom.disable();
  101. //监听地图单击事件
  102. map2DViewer.map.on("click", _this._onClickPoint, this);
  103. //监听鼠标移动事件
  104. map2DViewer.map.on("mousemove", _this._mousemove, this);
  105. },
  106. _mousemove: function (e) {
  107. if (mousemoveStatus) {
  108. var _this = this;
  109. e.latlng = _this.latlngRectifying(e);
  110. if (_this.moveMarker) {
  111. _this.moveMarker.setLatLng(e.latlng);
  112. if (_this._measureObjs[_this._lC].polyline) {
  113. _this._measureObjs[_this._lC].measurePoints.push([
  114. e.latlng.lat,
  115. e.latlng.lng,
  116. ]);
  117. var points = _this._measureObjs[_this._lC].measurePoints;
  118. if (_this._measureObjs[_this._lC].measurePoints.length == 2) {
  119. var radius = L.latLng(points[1]).distanceTo(L.latLng(points[0]));
  120. _this._measureObjs[_this._lC].circle.setRadius(radius);
  121. } else if (
  122. _this._measureObjs[_this._lC].measurePoints.length == 3 &&
  123. e.latlng.indexMarker == 1
  124. ) {
  125. _this._measureObjs[_this._lC].circle.setStyle({
  126. opacity: 1,
  127. });
  128. } else {
  129. _this._measureObjs[_this._lC].circle.setStyle({
  130. opacity: 0,
  131. });
  132. }
  133. if (_this._measureObjs[_this._lC].measurePoints.length > 2) {
  134. var coors = _.clone(_this._measureObjs[_this._lC].measurePoints);
  135. coors.push(
  136. _this._measureObjs[_this._lC].measurePoints[
  137. _this._measureObjs[_this._lC].measurePoints.length - 1
  138. ]
  139. );
  140. if (_this._measureObjs[_this._lC].polygon) {
  141. _this._measureObjs[_this._lC].polygon.setLatLngs([coors]);
  142. } else {
  143. _this._measureObjs[_this._lC].polygon = L.polygon([coors], {
  144. color: "#fff",
  145. weight: 0,
  146. fillColor: "#fff",
  147. fillOpacity: 0.3,
  148. }).addTo(_this._measureObjs[_this._lC]);
  149. }
  150. }
  151. _this._measureObjs[_this._lC].polyline.setLatLngs(points);
  152. //实时计算测量数据
  153. var moveData = _.clone(points);
  154. _this._moveInfo(moveData);
  155. _this._measureObjs[_this._lC].measurePoints.pop();
  156. }
  157. } else {
  158. _this.moveMarker = L.circleMarker(e.latlng, {
  159. radius: 10,
  160. color: "#f00",
  161. }).addTo(_this._map);
  162. }
  163. } else {
  164. map2DViewer.map.off("mousemove");
  165. console.log("lmx");
  166. }
  167. },
  168. //测量时,根据鼠标移动点位信息
  169. _moveInfo: function (data) {
  170. var _this = this;
  171. var lineDistance = 0;
  172. for (var i = 1; i < data.length; i++) {
  173. var curcoor = data[i];
  174. var prePoint = data[i - 1];
  175. lineDistance = L.latLng(curcoor).distanceTo(prePoint);
  176. var lineDistanceStr =
  177. lineDistance > 1000
  178. ? (lineDistance / 1000).toFixed(2) + "公里"
  179. : Math.ceil(lineDistance) + "米";
  180. if (i == data.length - 1) {
  181. var pointAngle = L.Util.getAngleByLatLng(
  182. prePoint[1],
  183. prePoint[0],
  184. curcoor[1],
  185. curcoor[0]
  186. );
  187. }
  188. }
  189. //添加分段信息
  190. var oLabelObj = L.DomUtil.create("div", "measure-content");
  191. var pointText = L.DomUtil.create("span", "measure-result-text");
  192. pointText.innerHTML = lineDistanceStr;
  193. oLabelObj.appendChild(pointText);
  194. if (data.length > 1) {
  195. if (_this.tmpMarkers[data.length - 2]) {
  196. //如果最后一个点存在,更新最后一个点信息
  197. _this.tmpMarkers[data.length - 2].setLatLng(data[data.length - 1]);
  198. _this.tmpMarkers[data.length - 2].label.setContent(oLabelObj);
  199. } else {
  200. //如果最后一个点不存在,创建点信息
  201. var marker = L.marker(data[data.length - 1], {
  202. icon: L.divIcon({ className: "measuremarker", iconSize: [4, 4] }),
  203. });
  204. marker.bindLabel(oLabelObj, {
  205. noHide: true,
  206. clickable: true,
  207. className: "measure-tip",
  208. offset: [0, 0],
  209. });
  210. marker.addTo(_this._measureObjs[_this._lC]);
  211. _this.tmpMarkers.push(marker);
  212. }
  213. }
  214. },
  215. /**
  216. * 通过坐标点计算面积
  217. * @type {Function}
  218. * @returns {Number} 面积
  219. * @private
  220. */
  221. _getArea: function (_lc) {
  222. var _this = this;
  223. var latLngs = _this._measureObjs[_lc].measurePoints;
  224. var pointsCount = latLngs.length,
  225. area = 0.0,
  226. d2r = Math.PI / 180,
  227. p1,
  228. p2;
  229. if (pointsCount > 2) {
  230. for (var i = 0; i < pointsCount; i++) {
  231. p1 = latLngs[i];
  232. p2 = latLngs[(i + 1) % pointsCount];
  233. area +=
  234. (p2[1] - p1[1]) *
  235. d2r *
  236. (2 + Math.sin(p1[0] * d2r) + Math.sin(p2[0] * d2r));
  237. }
  238. area = (area * 6378137.0 * 6378137.0) / 2.0;
  239. }
  240. area = Math.abs(area);
  241. if (area > 1000000) {
  242. area = (area * 0.000001).toFixed(2) + " 平方公里";
  243. } else {
  244. area = area.toFixed(2) + " 米&sup2;";
  245. }
  246. return area;
  247. },
  248. latlngRectifying: function (e) {
  249. var _this = this;
  250. var curLatlngs = [];
  251. var curpx = e.layerPoint;
  252. var length = _this._measureObjs[_this._lC]
  253. ? _this._measureObjs[_this._lC].measurePoints.length
  254. : 0;
  255. var latlng = e.latlng;
  256. if (length > 0) {
  257. curLatlngs.push({
  258. latlng: _this._measureObjs[_this._lC].measurePoints[0],
  259. px: _this._map.latLngToLayerPoint(
  260. _this._measureObjs[_this._lC].measurePoints[0]
  261. ),
  262. });
  263. curLatlngs.push({
  264. latlng: _this._measureObjs[_this._lC].measurePoints[length - 1],
  265. px: _this._map.latLngToLayerPoint(
  266. _this._measureObjs[_this._lC].measurePoints[length - 1]
  267. ),
  268. });
  269. }
  270. for (var i = 0; i < curLatlngs.length; i++) {
  271. var dispx = Math.sqrt(
  272. (curpx.x - curLatlngs[i].px.x) * (curpx.x - curLatlngs[i].px.x) +
  273. (curpx.y - curLatlngs[i].px.y) * (curpx.y - curLatlngs[i].px.y)
  274. );
  275. if (dispx < 10) {
  276. latlng = L.latLng(curLatlngs[i].latlng);
  277. latlng.reset = true;
  278. latlng.indexMarker = i;
  279. break;
  280. }
  281. }
  282. return latlng;
  283. },
  284. _onClickPoint: function (e) {
  285. var _this = this;
  286. e.latlng = _this.latlngRectifying(e);
  287. if (!e.latlng.reset) {
  288. _this._measureObjs[_this._lC].measurePoints.push([
  289. e.latlng.lat,
  290. e.latlng.lng,
  291. ]);
  292. } else {
  293. if (e.latlng.indexMarker == 0) {
  294. if (_this._measureObjs[_this._lC].polygon) {
  295. _this._measureObjs[_this._lC].measurePoints.push([
  296. e.latlng.lat,
  297. e.latlng.lng,
  298. ]);
  299. _this._measureObjs[_this._lC].polygon.setLatLngs(
  300. _this._measureObjs[_this._lC].measurePoints
  301. );
  302. _this._measureObjs[_this._lC].polygon.setStyle({
  303. fillColor: "#f00",
  304. color: "#f00",
  305. weight: 0,
  306. fillOpacity: 0.3,
  307. });
  308. } else {
  309. _this._measureObjs[_this._lC].removeLayer(
  310. _this._measureObjs[_this._lC].circle
  311. );
  312. _this._measureObjs[_this._lC].removeLayer(
  313. _this._measureObjs[_this._lC].polyline
  314. );
  315. }
  316. } else {
  317. _this._measureObjs[_this._lC].removeLayer(
  318. _this._measureObjs[_this._lC].polygon
  319. );
  320. _this._measureObjs[_this._lC].polygon = null;
  321. }
  322. _this._onFinishClick();
  323. }
  324. if (
  325. _this._measureObjs[_this._lC].measurePoints.length == 1 &&
  326. !_this._finished
  327. ) {
  328. var latlngs = [
  329. [e.latlng.lat, e.latlng.lng],
  330. [e.latlng.lat, e.latlng.lng],
  331. ];
  332. _this._measureObjs[_this._lC].polyline = L.polyline(latlngs, {
  333. color: "red",
  334. dashArray: 10,
  335. }).addTo(_this._measureObjs[_this._lC]);
  336. _this._measureObjs[_this._lC].circle = L.circle(
  337. [e.latlng.lat, e.latlng.lng],
  338. { radius: 0, color: "red", fillOpacity: 0 }
  339. ).addTo(_this._measureObjs[_this._lC]);
  340. } else {
  341. // _this._measureObjs[_this._lC].removeLayer(_this._measureObjs[_this._lC].circle);
  342. }
  343. },
  344. _onFinishClick: function () {
  345. _this = this;
  346. //关闭地图双击事件
  347. map2DViewer.map.doubleClickZoom.enable();
  348. //监听地图单击事件
  349. map2DViewer.map.off("click", _this._onClickPoint, this);
  350. //监听鼠标移动事件
  351. console.log("鼠标移动this --test", "_onFinishClick");
  352. map2DViewer.map.off("mousemove", _this._mousemove, this);
  353. _this.isNewElevation = true;
  354. _this._finished = true;
  355. _this._map.removeLayer(_this.moveMarker);
  356. _this.moveMarker = null;
  357. for (var i = 0; i < _this.tmpMarkers.length; i++) {
  358. _this._measureObjs[_this._lC].removeLayer(_this.tmpMarkers[i]);
  359. }
  360. _this.tmpMarkers = [];
  361. //根据点集合渲染marker点
  362. var coorslength = _this._measureObjs[_this._lC].measurePoints.length;
  363. _this._measureObjs[_this._lC].markerObjs = [];
  364. var lineDistance = 0;
  365. if (coorslength > 1) {
  366. for (var i = 0; i < coorslength; i++) {
  367. var curcoor = _this._measureObjs[_this._lC].measurePoints[i];
  368. var marker = L.marker(curcoor, {
  369. draggable: true,
  370. icon: L.divIcon({ className: "measuremarker", iconSize: [10, 10] }),
  371. });
  372. if (i > 0) {
  373. var prePoint = _this._measureObjs[_this._lC].measurePoints[i - 1];
  374. lineDistance = L.latLng(curcoor).distanceTo(prePoint);
  375. var lineDistanceStr =
  376. lineDistance > 1000
  377. ? (lineDistance / 1000).toFixed(2) + "公里"
  378. : Math.ceil(lineDistance) + "米";
  379. var pointAngle = L.Util.getAngleByLatLng(
  380. prePoint[1],
  381. prePoint[0],
  382. curcoor[1],
  383. curcoor[0]
  384. );
  385. //添加分段信息
  386. var oLabelObj = L.DomUtil.create("div", "measure-content");
  387. var delLabel = L.DomUtil.create("div", "measure-ico-del");
  388. var saveLabel = L.DomUtil.create("div", "measure-ico-save");
  389. var pointText = L.DomUtil.create("span", "measure-result-text");
  390. pointText.innerHTML = lineDistanceStr;
  391. delLabel.lC = _this._lC;
  392. saveLabel.lC = _this._lC;
  393. L.DomEvent.on(delLabel, "click", function (e) {
  394. L.DomEvent.stopPropagation(e);
  395. _this.del(delLabel.lC);
  396. });
  397. L.DomEvent.on(saveLabel, "click", function (e) {
  398. L.DomEvent.stopPropagation(e);
  399. map2DViewer.map.fire("draw-result", {
  400. distance: _this._measureObjs[delLabel.lC].distance,
  401. points: _this._measureObjs[delLabel.lC].measurePoints,
  402. area: _this._measureObjs[delLabel.lC].area || 0,
  403. LC: delLabel.lC,
  404. });
  405. });
  406. if (i == coorslength - 1) {
  407. //测量面积
  408. if (_this._measureObjs[_this._lC].polygon) {
  409. var area = _this._getArea(_this._lC);
  410. _this._measureObjs[_this._lC].area = area;
  411. pointText.innerHTML =
  412. lineDistanceStr + "<br/>" + area;
  413. }
  414. oLabelObj.appendChild(delLabel);
  415. oLabelObj.appendChild(saveLabel);
  416. }
  417. oLabelObj.appendChild(pointText);
  418. marker.bindLabel(oLabelObj, {
  419. noHide: true,
  420. clickable: true,
  421. className: "measure-tip",
  422. offset: [0, 0],
  423. });
  424. }
  425. marker.LC = _this._lC;
  426. marker.LIndex = i;
  427. marker.addTo(_this._measureObjs[_this._lC]);
  428. _this._measureObjs[_this._lC].markerObjs.push(marker);
  429. if (!(i == 0 && _this._measureObjs[_this._lC].polygon)) {
  430. marker.on("move", function (e) {
  431. _this._renderMeasure({
  432. LC: e.target.LC,
  433. LIndex: e.target.LIndex,
  434. latlng: e.latlng,
  435. });
  436. });
  437. }
  438. }
  439. } else {
  440. var curcoor = _this._measureObjs[_this._lC].measurePoints[0];
  441. var myIcon = L.icon({
  442. iconUrl: this.options.iconUrl,
  443. iconSize: [25, 41],
  444. iconAnchor: [12, 41],
  445. });
  446. var marker = L.marker(curcoor, { draggable: true, icon: myIcon });
  447. var oLabelObj = L.DomUtil.create("div", "measure-content");
  448. var delLabel = L.DomUtil.create("div", "measure-ico-del");
  449. var saveLabel = L.DomUtil.create("div", "measure-ico-save");
  450. delLabel.lC = _this._lC;
  451. saveLabel.lC = _this._lC;
  452. L.DomEvent.on(delLabel, "click", function (e) {
  453. L.DomEvent.stopPropagation(e);
  454. _this.del(delLabel.lC);
  455. });
  456. L.DomEvent.on(saveLabel, "click", function (e) {
  457. L.DomEvent.stopPropagation(e);
  458. map2DViewer.map.fire("draw-result", {
  459. distance: _this._measureObjs[delLabel.lC].distance,
  460. points: _this._measureObjs[delLabel.lC].measurePoints,
  461. area: _this._measureObjs[delLabel.lC].area || 0,
  462. LC: delLabel.lC,
  463. });
  464. });
  465. oLabelObj.appendChild(delLabel);
  466. oLabelObj.appendChild(saveLabel);
  467. marker.bindLabel(oLabelObj, {
  468. noHide: true,
  469. clickable: true,
  470. className: "measure-tip",
  471. offset: [0, 0],
  472. });
  473. marker.LC = _this._lC;
  474. marker.LIndex = i;
  475. marker.addTo(_this._measureObjs[_this._lC]);
  476. _this._measureObjs[_this._lC].markerObjs.push(marker);
  477. }
  478. _this._measureObjs[_this._lC].distance = lineDistance;
  479. },
  480. _renderMeasure: function (data) {
  481. var _this = this;
  482. var latlng = [data.latlng.lat, data.latlng.lng];
  483. if (_this._measureObjs[data.LC].measurePoints.length == 1) {
  484. return false;
  485. }
  486. _this._measureObjs[data.LC].measurePoints[data.LIndex] = latlng;
  487. var curlength = _this._measureObjs[data.LC].measurePoints.length;
  488. //如果是面,第一个点和最后一个点位置同时改变
  489. if (_this._measureObjs[data.LC].polygon) {
  490. if (data.LIndex == 0) {
  491. _this._measureObjs[data.LC].measurePoints[curlength - 1] = latlng;
  492. _this._measureObjs[_this._lC].markerObjs[curlength - 1].setLatLng(
  493. latlng
  494. );
  495. }
  496. if (data.LIndex == curlength - 1) {
  497. _this._measureObjs[data.LC].measurePoints[0] = latlng;
  498. _this._measureObjs[_this._lC].markerObjs[0].setLatLng(latlng);
  499. }
  500. }
  501. var points = _this._measureObjs[data.LC].measurePoints;
  502. //更新circle
  503. var radius = L.latLng(points[1]).distanceTo(L.latLng(points[0]));
  504. _this._measureObjs[data.LC].circle.setRadius(radius);
  505. _this._measureObjs[data.LC].circle.setLatLng(points[0]);
  506. if (points.length == 2) {
  507. _this._measureObjs[data.LC].circle.setStyle({
  508. opacity: 1,
  509. });
  510. }
  511. //更新线
  512. _this._measureObjs[data.LC].polyline.setLatLngs(points);
  513. //更新面
  514. if (_this._measureObjs[data.LC].polygon) {
  515. _this._measureObjs[data.LC].polygon.setLatLngs([points]);
  516. }
  517. var lineDistance = 0;
  518. var coorslength = _this._measureObjs[data.LC].measurePoints.length;
  519. for (var i = 1; i < coorslength; i++) {
  520. var curcoor = _this._measureObjs[data.LC].measurePoints[i];
  521. var prePoint = _this._measureObjs[data.LC].measurePoints[i - 1];
  522. lineDistance = L.latLng(curcoor).distanceTo(prePoint);
  523. var lineDistanceStr =
  524. lineDistance > 1000
  525. ? (lineDistance / 1000).toFixed(2) + "公里"
  526. : Math.ceil(lineDistance) + "米";
  527. var pointAngle = L.Util.getAngleByLatLng(
  528. prePoint[1],
  529. prePoint[0],
  530. curcoor[1],
  531. curcoor[0]
  532. );
  533. //添加分段信息
  534. var oLabelObj = L.DomUtil.create("div", "measure-content");
  535. var delLabel = L.DomUtil.create("div", "measure-ico-del");
  536. var saveLabel = L.DomUtil.create("div", "measure-ico-save");
  537. var pointText = L.DomUtil.create("span", "measure-result-text");
  538. pointText.innerHTML = lineDistanceStr;
  539. delLabel.lC = _this._lC;
  540. saveLabel.lC = _this._lC;
  541. L.DomEvent.on(delLabel, "click", function (e) {
  542. L.DomEvent.stopPropagation(e);
  543. _this.del(delLabel.lC);
  544. });
  545. L.DomEvent.on(saveLabel, "click", function (e) {
  546. L.DomEvent.stopPropagation(e);
  547. map2DViewer.map.fire("draw-result", {
  548. distance: _this._measureObjs[delLabel.lC].distance,
  549. points: _this._measureObjs[delLabel.lC].measurePoints,
  550. area: _this._measureObjs[delLabel.lC].area || 0,
  551. LC: delLabel.lC,
  552. });
  553. });
  554. if (i == coorslength - 1) {
  555. oLabelObj.appendChild(delLabel);
  556. oLabelObj.appendChild(saveLabel);
  557. //测量面积
  558. try {
  559. if (_this._measureObjs[data.LC].polygon) {
  560. var area = _this._getArea(data.LC);
  561. _this._measureObjs[data.LC].area;
  562. pointText.innerHTML =
  563. lineDistanceStr + "<br/>" + area;
  564. }
  565. } catch (e) {
  566. console.error("leaflet.draw.js 测量面积error:", e);
  567. }
  568. }
  569. oLabelObj.appendChild(pointText);
  570. _this._measureObjs[_this._lC].distance = lineDistance;
  571. _this._measureObjs[data.LC].markerObjs[i].label.setContent(oLabelObj);
  572. }
  573. },
  574. /**
  575. * 删除对应lC的测距
  576. */
  577. del: function (lC) {
  578. var _this = this;
  579. if (_this._measureObjs[lC]) {
  580. _this._measureGroup.removeLayer(_this._measureObjs[lC]);
  581. delete _this._measureObjs[lC];
  582. }
  583. },
  584. });
  585. /**
  586. * 测量工具
  587. */
  588. map2DViewer.drawToolFire = function (data) {
  589. console.log(data);
  590. };
  591. map2DViewer.setDrawTool = function (options) {
  592. mousemoveStatus = true;
  593. var defaultData = {
  594. action: "add",
  595. position: "topleft",
  596. offset: [10, 10],
  597. background: "#fff",
  598. color: "#000",
  599. font_size: "14px",
  600. closeButton: true,
  601. };
  602. _.merge(defaultData, options);
  603. switch (defaultData.action) {
  604. case "add":
  605. this.measureTool = new L.Control.Draw({
  606. position: defaultData.position,
  607. offset: defaultData.offset,
  608. background: defaultData.background,
  609. color: defaultData.color,
  610. font_size: defaultData.font_size,
  611. closeButton: defaultData.closeButton,
  612. iconUrl:
  613. "/static/plugins/draw-plugin/images/marker-icon.png",
  614. }).addTo(this.map);
  615. this.map.on("draw-result", map2DViewer.drawToolFire);
  616. return this.distanceTool;
  617. break;
  618. case "remove":
  619. this.map.off("draw-result", map2DViewer.drawToolFire);
  620. if (this.measureTool) {
  621. // 移除绘制的图形
  622. this.measureTool._measureGroup &&
  623. this.measureTool._measureGroup.remove();
  624. mousemoveStatus = false;
  625. map2DViewer.map.doubleClickZoom.disable();
  626. map2DViewer.map.off("click");
  627. if (this.measureTool.moveMarker) {
  628. this.map.removeLayer(this.measureTool.moveMarker);
  629. this.measureTool.moveMarker = null;
  630. }
  631. this.measureTool.remove();
  632. this.measureTool = null
  633. map2DViewer.measureTool = null;
  634. }
  635. break;
  636. case "start":
  637. this.measureTool.start();
  638. break;
  639. }
  640. };