leaflet.AltitudeTool.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. L.Control.AltitudeTool = L.Control.extend({
  2. /**
  3. * 线条统计
  4. * @type {Number}
  5. * @default 0
  6. * @private
  7. */
  8. _lC:0,
  9. /**
  10. * 量算集合
  11. * @type {Object}
  12. * @default {}
  13. * @private
  14. */
  15. _altitudeObjs:{},
  16. /**
  17. * 点和线容器
  18. * @type {Object}
  19. * @private
  20. */
  21. _altitudeGroup: new L.FeatureGroup(),
  22. /**
  23. * 正在进行量算的
  24. * @type {[type]}
  25. */
  26. _curElevIC:null,
  27. /**
  28. * 是否是新的量算事件
  29. * @type {Boolean}
  30. * @default true
  31. * @private
  32. */
  33. _isNewaltitude:true,
  34. /**
  35. * 布局参数
  36. * @type {Object}
  37. */
  38. options:{
  39. position:'topright',
  40. autoZIndex:true,
  41. offset:[10,10]
  42. },
  43. initialize:function(options){
  44. L.setOptions(this, options);
  45. return this;
  46. },
  47. onAdd:function(map){
  48. this._map = map;
  49. this._altitudeGroup.addTo(this._map);
  50. this._createControl();
  51. switch(this.options.position){
  52. case 'topleft':
  53. this._container.style.marginLeft = this.options.offset[0]+'px';
  54. this._container.style.marginTop = this.options.offset[1]+'px';
  55. break;
  56. case 'topright':
  57. this._container.style.marginRight = this.options.offset[0]+'px';
  58. this._container.style.marginTop = this.options.offset[1]+'px';
  59. break;
  60. case 'bottomleft':
  61. this._container.style.marginLeft = this.options.offset[0]+'px';
  62. this._container.style.marginBottom = this.options.offset[1]+'px';
  63. break;
  64. case 'bottomright':
  65. this._container.style.marginRight = this.options.offset[0]+'px';
  66. this._container.style.marginBottom = this.options.offset[1]+'px';
  67. break;
  68. }
  69. return this._container;
  70. },
  71. _createControl:function(){
  72. var _this = this;
  73. this._container = L.DomUtil.create('div','leaflet-bar leaflet-control-altitude-tool');
  74. var link = L.DomUtil.create('a','leaflet-control-altitude-tool-link',this._container);
  75. link.title = '高程量算';
  76. link.innerHTML = 'G'
  77. L.DomUtil.create('span','',link);
  78. L.DomEvent.on(this._container,'contextmenu',L.DomEvent.stopPropagation);
  79. L.DomEvent
  80. .on(link,'click', L.DomEvent.stopPropagation)
  81. .on(link,'click', L.DomEvent.preventDefault)
  82. .on(link,'click',function(){
  83. if(!_this._isNewaltitude){
  84. _this.remove();
  85. }else {
  86. _this._isNewaltitude = false;
  87. L.DomUtil.addClass(_this._container,'active');
  88. _this._map.doubleClickZoom.disable();
  89. _this._map.on('click',_this._onClickPoint,_this);
  90. if(L.Browser.ie || L.Browser.firefox){
  91. _this._map.getContainer().style.cursor = 'url('+L.DefaultImagePath+'/cur-ruler.cur),auto';
  92. }else{
  93. _this._map.getContainer().style.cursor = 'url('+L.DefaultImagePath+'/cur-ruler.cur) 5 5,auto';
  94. }
  95. }
  96. })
  97. },
  98. /**
  99. * 鼠标单击事件
  100. * @param e
  101. * @returns {boolean}
  102. * @private
  103. */
  104. _onClickPoint:function(e){
  105. var _this = this;
  106. _this._lC++;
  107. _this._altitudeObjs[_this._lC] = new L.FeatureGroup();
  108. _this._altitudeObjs[_this._lC].addTo(_this._altitudeGroup);
  109. //e.latlng
  110. _this._buildMarker({latlng: e.latlng});
  111. },
  112. /**
  113. * 删除对应iC的测距
  114. */
  115. del:function(event,iC){
  116. var _this = this;
  117. var event = event || window.event;
  118. L.DomEvent.stop(event);
  119. _this._altitudeGroup.removeLayer(_this._altitudeObjs[iC]);
  120. delete _this._altitudeObjs[iC];
  121. },
  122. /**
  123. * 清除所有的测距
  124. * @type {Function}
  125. */
  126. clearAllelevation:function(){
  127. var _this = this;
  128. _this._altitudeGroup.clearLayers();
  129. _this._altitudeObjs = {};
  130. },
  131. /**
  132. * 进行剖面量算
  133. * @param {[type]} event [description]
  134. * @param {[type]} iC [description]
  135. * @return {[type]} [description]
  136. */
  137. run:function(latlng,callBackFunc){
  138. var _this = this;
  139. var url = map23DConfig.altitudeServerUrl+'/terrain/elevation';
  140. var ajaxData = {
  141. lat:latlng.lat,
  142. lon:latlng.lng,
  143. zoom:_this._map.getZoom(),
  144. delta_zoom:0
  145. };
  146. $.ajax({
  147. type:"post",
  148. dataType:'json',
  149. data:ajaxData,
  150. url:url,
  151. success:function(data){
  152. callBackFunc(data);
  153. }
  154. });
  155. },
  156. /**
  157. * 创建一个marker 并返回该marker
  158. * @param obj
  159. * @returns {L.Marker}
  160. * @private
  161. */
  162. _buildMarker:function(obj){
  163. var _this = this;
  164. _this.run(obj.latlng,function(data){
  165. var altitudeLabel = L.DomUtil.create('span','altitude-span');
  166. altitudeLabel.lid = _this._lC
  167. altitudeLabel.style.color = 'red';
  168. altitudeLabel.innerHTML = '高程:'+data.data+ '米';
  169. L.DomEvent.on(altitudeLabel,'dblclick',function(e){
  170. L.DomEvent.stopPropagation(e);
  171. var lid = e.target? e.target.lid: e.srcElement.lid;
  172. _this.del(e,lid)
  173. });
  174. new L.Marker(
  175. obj.latlng,
  176. {icon: L.icon({
  177. iconUrl: L.DefaultImagePath+'/icon-linePoint.png',
  178. iconSize: [15, 15],
  179. iconAnchor: [7, 7],
  180. popupAnchor: [0, -7]
  181. }),
  182. clickable:false
  183. }
  184. ).bindLabel(altitudeLabel,{
  185. noHide:true,
  186. }).addTo(_this._altitudeObjs[_this._lC]);
  187. });
  188. _this.remove();
  189. },
  190. /**
  191. * 计算完成
  192. * @type {Function}
  193. */
  194. remove:function(){
  195. var _this = this;
  196. _this._map.doubleClickZoom.enable();
  197. _this._map.getContainer().style.cursor = 'auto';
  198. _this._isNewaltitude = true;
  199. _this._map.off('click',_this._onClickPoint,this);
  200. L.DomUtil.removeClass(_this._container,'active');
  201. }
  202. });
  203. L.control.altitudeTool = function(options){
  204. return new L.Control.AltitudeTool(options);
  205. }
  206. /**
  207. * 高程量算
  208. */
  209. map2DViewer.altitudeToolsFire = function(data) {};
  210. map2DViewer.setAltitudeTools = function(options) {
  211. var defaultData = {
  212. action: 'add',
  213. position: 'topleft',
  214. offset: [10, 10]
  215. }
  216. _.merge(defaultData, options);
  217. switch (defaultData.action) {
  218. case 'add':
  219. this.altitudeTools = new L.Control.AltitudeTool({
  220. position: defaultData.position,
  221. offset: defaultData.offset
  222. }).addTo(this.map);
  223. this.map.on('measure-distance-result', map2DViewer.altitudeToolsFire);
  224. return this.altitudeTools;
  225. break;
  226. case 'remove':
  227. this.altitudeTools.cleanAllMeasure();
  228. this.map.removeControl(this.altitudeTools);
  229. this.map.off('measure-distance-result', map2DViewer.altitudeToolsFire);
  230. break;
  231. }
  232. };