leaflet.draw.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. _this._map.doubleClickZoom.disable();
  101. //监听地图单击事件
  102. _this._map.on("click", _this._onClickPoint, this);
  103. //监听鼠标移动事件
  104. _this._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. this._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) + " 平方米";
  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. _this._map.doubleClickZoom.enable();
  348. //监听地图单击事件
  349. _this._map.off("click", _this._onClickPoint, this);
  350. //监听鼠标移动事件
  351. _this._map.off("mousemove", _this._mousemove, this);
  352. _this.isNewElevation = true;
  353. _this._finished = true;
  354. _this._map.removeLayer(_this.moveMarker);
  355. _this.moveMarker = null;
  356. for (var i = 0; i < _this.tmpMarkers.length; i++) {
  357. _this._measureObjs[_this._lC].removeLayer(_this.tmpMarkers[i]);
  358. }
  359. _this.tmpMarkers = [];
  360. //根据点集合渲染marker点
  361. var coorslength = _this._measureObjs[_this._lC].measurePoints.length;
  362. _this._measureObjs[_this._lC].markerObjs = [];
  363. var lineDistance = 0;
  364. if (coorslength > 1) {
  365. for (var i = 0; i < coorslength; i++) {
  366. var curcoor = _this._measureObjs[_this._lC].measurePoints[i];
  367. var marker = L.marker(curcoor, {
  368. draggable: true,
  369. icon: L.divIcon({ className: "measuremarker", iconSize: [10, 10] }),
  370. });
  371. if (i > 0) {
  372. var prePoint = _this._measureObjs[_this._lC].measurePoints[i - 1];
  373. lineDistance += L.latLng(curcoor).distanceTo(prePoint);
  374. var lineDistanceStr =
  375. lineDistance > 1000
  376. ? (lineDistance / 1000).toFixed(2) + "公里"
  377. : Math.ceil(lineDistance) + "米";
  378. var pointAngle = L.Util.getAngleByLatLng(
  379. prePoint[1],
  380. prePoint[0],
  381. curcoor[1],
  382. curcoor[0]
  383. );
  384. //添加分段信息
  385. var oLabelObj = L.DomUtil.create("div", "measure-content");
  386. var delLabel = L.DomUtil.create("div", "measure-ico-del");
  387. var saveLabel = L.DomUtil.create("div", "measure-ico-save");
  388. var listLabel = L.DomUtil.create("div", "measure-ico-list");
  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. listLabel.lC = _this._lC;
  394. L.DomEvent.on(delLabel, "click", function (e) {
  395. L.DomEvent.stopPropagation(e);
  396. _this.del(delLabel.lC);
  397. });
  398. L.DomEvent.on(saveLabel, "click", function (e) {
  399. L.DomEvent.stopPropagation(e);
  400. _this._map.fire("draw-result", {
  401. distance: _this._measureObjs[delLabel.lC].distance,
  402. points: _this._measureObjs[delLabel.lC].measurePoints,
  403. area: _this._measureObjs[delLabel.lC].area || 0,
  404. LC: delLabel.lC,
  405. });
  406. });
  407. L.DomEvent.on(listLabel, "click", function (e) {
  408. L.DomEvent.stopPropagation(e);
  409. _this._map.fire("box-select", {
  410. distance: _this._measureObjs[delLabel.lC].distance,
  411. points: _this._measureObjs[delLabel.lC].measurePoints,
  412. area: _this._measureObjs[delLabel.lC].area || 0,
  413. LC: delLabel.lC,
  414. });
  415. });
  416. if (i == coorslength - 1) {
  417. //测量面积
  418. if (_this._measureObjs[_this._lC].polygon) {
  419. var area = _this._getArea(_this._lC);
  420. _this._measureObjs[_this._lC].area = area;
  421. pointText.innerHTML =
  422. lineDistanceStr + "<br/>" + area;
  423. }
  424. oLabelObj.appendChild(delLabel);
  425. oLabelObj.appendChild(saveLabel);
  426. oLabelObj.appendChild(listLabel);
  427. }
  428. oLabelObj.appendChild(pointText);
  429. marker.bindLabel(oLabelObj, {
  430. noHide: true,
  431. clickable: true,
  432. className: "measure-tip",
  433. offset: [0, 0],
  434. });
  435. }
  436. marker.LC = _this._lC;
  437. marker.LIndex = i;
  438. marker.addTo(_this._measureObjs[_this._lC]);
  439. _this._measureObjs[_this._lC].markerObjs.push(marker);
  440. if (!(i == 0 && _this._measureObjs[_this._lC].polygon)) {
  441. marker.on("move", function (e) {
  442. _this._renderMeasure({
  443. LC: e.target.LC,
  444. LIndex: e.target.LIndex,
  445. latlng: e.latlng,
  446. });
  447. });
  448. }
  449. }
  450. } else {
  451. var curcoor = _this._measureObjs[_this._lC].measurePoints[0];
  452. var myIcon = L.icon({
  453. iconUrl: this.options.iconUrl,
  454. iconSize: [25, 41],
  455. iconAnchor: [12, 41],
  456. });
  457. var marker = L.marker(curcoor, { draggable: true, icon: myIcon });
  458. var oLabelObj = L.DomUtil.create("div", "measure-content");
  459. var delLabel = L.DomUtil.create("div", "measure-ico-del");
  460. var saveLabel = L.DomUtil.create("div", "measure-ico-save");
  461. var listLabel = L.DomUtil.create("div", "measure-ico-list");
  462. delLabel.lC = _this._lC;
  463. saveLabel.lC = _this._lC;
  464. listLabel.lC = _this._lC;
  465. L.DomEvent.on(delLabel, "click", function (e) {
  466. L.DomEvent.stopPropagation(e);
  467. _this.del(delLabel.lC);
  468. });
  469. L.DomEvent.on(saveLabel, "click", function (e) {
  470. L.DomEvent.stopPropagation(e);
  471. _this._map.fire("draw-result", {
  472. distance: _this._measureObjs[delLabel.lC].distance,
  473. points: _this._measureObjs[delLabel.lC].measurePoints,
  474. area: _this._measureObjs[delLabel.lC].area || 0,
  475. LC: delLabel.lC,
  476. });
  477. });
  478. L.DomEvent.on(listLabel, "click", function (e) {
  479. L.DomEvent.stopPropagation(e);
  480. _this._map.fire("box-select", {
  481. distance: _this._measureObjs[delLabel.lC].distance,
  482. points: _this._measureObjs[delLabel.lC].measurePoints,
  483. area: _this._measureObjs[delLabel.lC].area || 0,
  484. LC: delLabel.lC,
  485. });
  486. });
  487. oLabelObj.appendChild(delLabel);
  488. oLabelObj.appendChild(saveLabel);
  489. oLabelObj.appendChild(listLabel);
  490. marker.bindLabel(oLabelObj, {
  491. noHide: true,
  492. clickable: true,
  493. className: "measure-tip",
  494. offset: [0, 0],
  495. });
  496. marker.LC = _this._lC;
  497. marker.LIndex = i;
  498. marker.addTo(_this._measureObjs[_this._lC]);
  499. _this._measureObjs[_this._lC].markerObjs.push(marker);
  500. }
  501. _this._measureObjs[_this._lC].distance = lineDistance;
  502. },
  503. _renderMeasure: function (data) {
  504. var _this = this;
  505. var latlng = [data.latlng.lat, data.latlng.lng];
  506. if (_this._measureObjs[data.LC].measurePoints.length == 1) {
  507. return false;
  508. }
  509. _this._measureObjs[data.LC].measurePoints[data.LIndex] = latlng;
  510. var curlength = _this._measureObjs[data.LC].measurePoints.length;
  511. //如果是面,第一个点和最后一个点位置同时改变
  512. if (_this._measureObjs[data.LC].polygon) {
  513. if (data.LIndex == 0) {
  514. _this._measureObjs[data.LC].measurePoints[curlength - 1] = latlng;
  515. _this._measureObjs[_this._lC].markerObjs[curlength - 1].setLatLng(
  516. latlng
  517. );
  518. }
  519. if (data.LIndex == curlength - 1) {
  520. _this._measureObjs[data.LC].measurePoints[0] = latlng;
  521. _this._measureObjs[_this._lC].markerObjs[0].setLatLng(latlng);
  522. }
  523. }
  524. var points = _this._measureObjs[data.LC].measurePoints;
  525. //更新circle
  526. var radius = L.latLng(points[1]).distanceTo(L.latLng(points[0]));
  527. _this._measureObjs[data.LC].circle.setRadius(radius);
  528. _this._measureObjs[data.LC].circle.setLatLng(points[0]);
  529. if (points.length == 2) {
  530. _this._measureObjs[data.LC].circle.setStyle({
  531. opacity: 1,
  532. });
  533. }
  534. //更新线
  535. _this._measureObjs[data.LC].polyline.setLatLngs(points);
  536. //更新面
  537. if (_this._measureObjs[data.LC].polygon) {
  538. _this._measureObjs[data.LC].polygon.setLatLngs([points]);
  539. }
  540. var lineDistance = 0;
  541. var coorslength = _this._measureObjs[data.LC].measurePoints.length;
  542. for (var i = 1; i < coorslength; i++) {
  543. var curcoor = _this._measureObjs[data.LC].measurePoints[i];
  544. var prePoint = _this._measureObjs[data.LC].measurePoints[i - 1];
  545. lineDistance += L.latLng(curcoor).distanceTo(prePoint);
  546. var lineDistanceStr =
  547. lineDistance > 1000
  548. ? (lineDistance / 1000).toFixed(2) + "公里"
  549. : Math.ceil(lineDistance) + "米";
  550. var pointAngle = L.Util.getAngleByLatLng(
  551. prePoint[1],
  552. prePoint[0],
  553. curcoor[1],
  554. curcoor[0]
  555. );
  556. //添加分段信息
  557. var oLabelObj = L.DomUtil.create("div", "measure-content");
  558. var delLabel = L.DomUtil.create("div", "measure-ico-del");
  559. var saveLabel = L.DomUtil.create("div", "measure-ico-save");
  560. var listLabel = L.DomUtil.create("div", "measure-ico-list");
  561. var pointText = L.DomUtil.create("span", "measure-result-text");
  562. pointText.innerHTML = lineDistanceStr;
  563. delLabel.lC = _this._lC;
  564. saveLabel.lC = _this._lC;
  565. listLabel.lC = _this._lC;
  566. L.DomEvent.on(delLabel, "click", function (e) {
  567. L.DomEvent.stopPropagation(e);
  568. _this.del(delLabel.lC);
  569. });
  570. L.DomEvent.on(saveLabel, "click", function (e) {
  571. L.DomEvent.stopPropagation(e);
  572. _this._map.fire("draw-result", {
  573. distance: _this._measureObjs[delLabel.lC].distance,
  574. points: _this._measureObjs[delLabel.lC].measurePoints,
  575. area: _this._measureObjs[delLabel.lC].area || 0,
  576. LC: delLabel.lC,
  577. });
  578. });
  579. L.DomEvent.on(listLabel, "click", function (e) {
  580. L.DomEvent.stopPropagation(e);
  581. _this._map.fire("box-select", {
  582. distance: _this._measureObjs[delLabel.lC].distance,
  583. points: _this._measureObjs[delLabel.lC].measurePoints,
  584. area: _this._measureObjs[delLabel.lC].area || 0,
  585. LC: delLabel.lC,
  586. });
  587. });
  588. if (i == coorslength - 1) {
  589. oLabelObj.appendChild(delLabel);
  590. oLabelObj.appendChild(saveLabel);
  591. oLabelObj.appendChild(listLabel);
  592. //测量面积
  593. try {
  594. if (_this._measureObjs[data.LC].polygon) {
  595. var area = _this._getArea(data.LC);
  596. _this._measureObjs[data.LC].area;
  597. pointText.innerHTML =
  598. lineDistanceStr + "<br/>" + area;
  599. }
  600. } catch (e) {
  601. // console.error("leaflet.draw.js 测量面积error:", e);
  602. }
  603. }
  604. oLabelObj.appendChild(pointText);
  605. _this._measureObjs[_this._lC].distance = lineDistance;
  606. _this._measureObjs[data.LC].markerObjs[i].label.setContent(oLabelObj);
  607. }
  608. },
  609. /**
  610. * 删除对应lC的测距
  611. */
  612. del: function (lC) {
  613. var _this = this;
  614. if (_this._measureObjs[lC]) {
  615. _this._measureGroup.removeLayer(_this._measureObjs[lC]);
  616. delete _this._measureObjs[lC];
  617. }
  618. },
  619. });
  620. /**
  621. * 测量工具
  622. */
  623. map2DViewer.drawToolFire = function (data) {
  624. };
  625. map2DViewer.setDrawTool = function (options) {
  626. mousemoveStatus = true;
  627. var defaultData = {
  628. action: "add",
  629. position: "topleft",
  630. offset: [10, 10],
  631. background: "#fff",
  632. color: "#000",
  633. font_size: "14px",
  634. closeButton: true,
  635. };
  636. _.merge(defaultData, options);
  637. switch (defaultData.action) {
  638. case "add":
  639. this._map = defaultData.map;
  640. this.measureTool = new L.Control.Draw({
  641. position: defaultData.position,
  642. offset: defaultData.offset,
  643. background: defaultData.background,
  644. color: defaultData.color,
  645. font_size: defaultData.font_size,
  646. closeButton: defaultData.closeButton,
  647. iconUrl:
  648. "/static/plugins/draw-plugin/images/marker-icon.png",
  649. }).addTo(this._map);
  650. this._map.on("draw-result", map2DViewer.drawToolFire);
  651. this._map.on("box-select", map2DViewer.boxSelect);
  652. return this.distanceTool;
  653. case "remove":
  654. this._map.off("draw-result", map2DViewer.drawToolFire);
  655. this._map.off("box-select", map2DViewer.boxSelect);
  656. if (this.measureTool) {
  657. // 移除绘制的图形
  658. this.measureTool._measureGroup &&
  659. this.measureTool._measureGroup.remove();
  660. mousemoveStatus = false;
  661. this._map.doubleClickZoom.disable();
  662. this._map.off("click");
  663. if (this.measureTool.moveMarker) {
  664. this._map.removeLayer(this.measureTool.moveMarker);
  665. this.measureTool.moveMarker = null;
  666. }
  667. this.measureTool.remove();
  668. this.measureTool = null
  669. map2DViewer.measureTool = null;
  670. }
  671. break;
  672. case "start":
  673. this.measureTool.start();
  674. break;
  675. }
  676. };