leaflet.measure.js 24 KB

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