leaflet.draw.js 21 KB

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