leaflet.STLatLngLayer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * 经纬网 网格显示
  3. * Author: Song.Huang
  4. * Date: 13-11-27
  5. * Time: 上午8:38
  6. */
  7. L.Control.STLatLngLayer = L.Control.extend({
  8. //是否初始化
  9. _initialized:false,
  10. _STLayerGroup : new L.FeatureGroup(),
  11. _STLayerTextGroup : new L.FeatureGroup(),
  12. _offsetX :[0,0,108000,72000,54000,27000,13500,10800,5400,2700,1350,675,450,225,135,90,45,30,20,10],
  13. _offsetY :[0,0,72000,54000,36000,18000,9000,7200,3600,1800,900,450,300,150,90,60,30,20,10,5],
  14. _options:{
  15. position:'topright',
  16. autoZIndex:true
  17. },
  18. initialize:function(options){
  19. L.setOptions(this,options);
  20. return this;
  21. },
  22. onAdd:function(map){
  23. this._map = map;
  24. this._createControl();
  25. this._map.on('cleanMap',this.disableST,this);
  26. return this._container;
  27. },
  28. _createControl:function(){
  29. var _this = this;
  30. this._container = L.DomUtil.create('div','leaflet-bar leaflet-control-st-latlng-layer');
  31. var link = L.DomUtil.create('a','leaflet-bar leaflet-control-st-latlng-layer-link',this._container);
  32. link.title = '经纬网';
  33. var span = L.DomUtil.create('span','',link);
  34. span.innerHTML = '经纬网';
  35. L.DomEvent
  36. .on(link,'click', L.DomEvent.stopPropagation)
  37. .on(link,'click', L.DomEvent.preventDefault)
  38. .on(link,'dblclick', L.DomEvent.preventDefault)
  39. .on(link,'dblclick', L.DomEvent.stopPropagation)
  40. .on(this._container,'contextmenu',L.DomEvent.stopPropagation)
  41. .on(link,'click',function(){
  42. if(_this._initialized){
  43. _this._initialized = false;
  44. L.DomUtil.removeClass(_this._container,'active');
  45. _this._hideSTLayer();
  46. _this._map.off('zoomend moveend resize',_this._addMapEvent,_this);
  47. }else {
  48. _this._initialized = true;
  49. L.DomUtil.addClass(_this._container,'active');
  50. _this._STLayerGroup.addTo(_this._map);
  51. _this._STLayerTextGroup.addTo(_this._map);
  52. _this._showSTLayer({zoom:_this._map.getZoom(),bounds:_this._map.getBounds(),group:_this._STLayerGroup,textGroup:_this._STLayerTextGroup});
  53. _this._map.on('zoomend moveend resize',_this._addMapEvent,_this);
  54. }
  55. });
  56. },
  57. disableST:function(){
  58. this.changStatus(false);
  59. },
  60. changStatus:function(status){
  61. var _this = this;
  62. if(!status){
  63. _this._initialized = false;
  64. L.DomUtil.removeClass(_this._container,'active');
  65. _this._hideSTLayer();
  66. _this._map.off('zoomend moveend resize',_this._addMapEvent,_this);
  67. }else {
  68. _this._initialized = true;
  69. L.DomUtil.addClass(_this._container,'active');
  70. _this._STLayerGroup.addTo(_this._map);
  71. _this._STLayerTextGroup.addTo(_this._map);
  72. _this._showSTLayer({zoom:_this._map.getZoom(),bounds:_this._map.getBounds(),group:_this._STLayerGroup,textGroup:_this._STLayerTextGroup});
  73. _this._map.on('zoomend moveend resize',_this._addMapEvent,_this);
  74. }
  75. },
  76. _addMapEvent:function(){
  77. var _this = this;
  78. if(_this._map.getZoom() > 1 && _this._map.getZoom() < 19){
  79. _this._STLayerGroup.clearLayers();
  80. _this._STLayerTextGroup.clearLayers();
  81. _this._showSTLayer({zoom:_this._map.getZoom(),bounds:_this._map.getBounds(),group:_this._STLayerGroup,textGroup:_this._STLayerTextGroup});
  82. }else {
  83. _this._STLayerGroup.clearLayers();
  84. _this._STLayerTextGroup.clearLayers();
  85. }
  86. },
  87. /**
  88. * 分格式化
  89. * @param {[type]} latlng [分]
  90. * @return {[type]} [description]
  91. */
  92. formatHMS: function (latlng){
  93. var lat = latlng.hasOwnProperty('lat')?latlng.lat:latlng[0];
  94. var lng = latlng.hasOwnProperty('lng')?latlng.lng:latlng[1];
  95. function setHMS(f){
  96. var h = parseInt(f/3600);
  97. var m = parseInt((f-h*3600)/60);
  98. var s = parseInt((f-h*3600-m*60));
  99. if(m.toString().length == 1){
  100. m = '0' + m.toString();
  101. }
  102. if(s.toString().length == 1){
  103. s = '0' + s.toString();
  104. }
  105. return h + '\u00b0' + m + '\u2032' + s + '\u2033';
  106. }
  107. var nLat = '';
  108. var nLng = '';
  109. if (lat < 0) {
  110. nLat = setHMS(lat * -1)+'S';
  111. } else {
  112. nLat = setHMS(lat)+'N';
  113. }
  114. if (lng < 0) {
  115. nLng = setHMS(lng * -1)+'W';
  116. } else {
  117. nLng = setHMS(lng)+'E';
  118. }
  119. return {lat:nLat,lng:nLng};
  120. },
  121. _showSTLayer:function(options){
  122. var _this = this;
  123. if(_this._map.getZoom() < 2){
  124. _this._STLayerGroup.clearLayers();
  125. return false;
  126. }
  127. var offsetX = _this._offsetX[options.zoom];
  128. var offsetY = _this._offsetY[options.zoom];
  129. var startSW = {lat:(parseInt(options.bounds._southWest.lat*3600/offsetY)-1)*offsetY,
  130. lng:(parseInt(options.bounds._southWest.lng*3600/offsetX)-1)*offsetX};
  131. var startNE = {lat:(parseInt(options.bounds._northEast.lat*3600/offsetY)+1)*offsetY,
  132. lng:(parseInt(options.bounds._northEast.lng*3600/offsetX)+1)*offsetX};
  133. //todo 这里需要做下优化 优化内容: 经纬度坐标文字更新和画线更新分离开来,让地图移动的时候让文字始终靠在界面边上
  134. for(var i = startSW.lat;i < startNE.lat;i+=offsetY){
  135. L.polyline([[i/3600,startSW.lng/3600],[i/3600,startNE.lng/3600]],{weight:1,color:'#04cfed',opacity:0.86}).on('mouseover',function(){
  136. this.setStyle({color:"#ff6600",weight:3,opacity:1})
  137. }).on('mouseout',function(){
  138. this.setStyle({color:"#04cfed",weight:1,opacity:0.86})
  139. }).addTo(options.group);
  140. //左
  141. if(L.Util.verifyLatLng(i/3600,options.bounds._southWest.lng)){
  142. //var lL = L.olabel(L.latLng(i/3600,options.bounds._southWest.lng),_this.formatHMS([i,options.bounds._southWest.lng*3600]).lat,{className:'st-label st-lable-left',offset:[0,0]});
  143. var lL = L.circleMarker(L.latLng(i/3600,options.bounds._southWest.lng),{
  144. radius:0,
  145. stroke:false
  146. })
  147. lL.addTo(options.textGroup);
  148. lL.bindTooltip(_this.formatHMS([i,options.bounds._southWest.lng*3600]).lat,{
  149. permanent:true,
  150. offset:[60,0]
  151. }).openTooltip();
  152. if(!L.Browser.ie){
  153. _this._map.on('move',function(){
  154. this.setLatLng(L.latLng(this.getLatLng().lat,_this._map.getBounds()._southWest.lng));
  155. },lL);
  156. }
  157. }
  158. //右
  159. if(L.Util.verifyLatLng(i/3600,options.bounds._northEast.lng)){
  160. //var rL = L.olabel(L.latLng(i/3600,options.bounds._northEast.lng), _this.formatHMS([i,options.bounds._northEast.lng*3600]).lat,{className:'st-label st-lable-right',offset:[0,0]});
  161. var rL = L.circleMarker(L.latLng(i/3600,options.bounds._northEast.lng),{
  162. radius:0,
  163. stroke:false
  164. });
  165. rL.addTo(options.textGroup);
  166. rL.bindTooltip(_this.formatHMS([i,options.bounds._northEast.lng*3600]).lat,{
  167. permanent:true,
  168. offset:[-10,0]
  169. }).openTooltip();
  170. if(!L.Browser.ie){
  171. _this._map.on('move',function(){
  172. this.setLatLng(L.latLng(this.getLatLng().lat,_this._map.getBounds()._northEast.lng));
  173. },rL);
  174. }
  175. }
  176. }
  177. for(var ii = startSW.lng;ii < startNE.lng;ii+=offsetX){
  178. L.polyline([[startSW.lat/3600,ii/3600],[startNE.lat/3600,ii/3600]],{weight:1,color:'#04cfed',opacity:0.86}).on('mouseover',function(){
  179. this.setStyle({color:"#ff6600",weight:3,opacity:1})
  180. }).on('mouseout',function(){
  181. this.setStyle({color:"#04cfed",weight:1,opacity:0.86})
  182. }).addTo(options.group);
  183. if(L.Util.verifyLatLng(options.bounds._southWest.lat,ii/3600)){
  184. //var bL = L.olabel(L.latLng(options.bounds._southWest.lat,ii/3600), _this.formatHMS([options.bounds._southWest.lng*3600,ii]).lng,{className:'st-label st-lable-bottom',offset:[0,0]});
  185. bL = L.circleMarker(L.latLng(options.bounds._southWest.lat,ii/3600),{
  186. radius:0,
  187. stroke:false
  188. });
  189. bL.addTo(options.textGroup);
  190. bL.bindTooltip(_this.formatHMS([options.bounds._southWest.lng*3600,ii]).lng,{
  191. permanent:true,
  192. offset:[0,-60]
  193. }).openTooltip();
  194. if(!L.Browser.ie){
  195. _this._map.on('move',function(){
  196. this.setLatLng(L.latLng(_this._map.getBounds()._southWest.lat,this.getLatLng().lng));
  197. },bL);
  198. }
  199. }
  200. if(L.Util.verifyLatLng(options.bounds._northEast.lat,ii/3600)){
  201. //var tL = L.olabel(L.latLng(options.bounds._northEast.lat,ii/3600), _this.formatHMS([options.bounds._northEast.lng*3600,ii]).lng,{className:'st-label st-lable-top',offset:[0,0]});
  202. tL = L.circleMarker(L.latLng(options.bounds._northEast.lat,ii/3600),{
  203. radius:0,
  204. stroke:false
  205. });
  206. tL.addTo(options.textGroup);
  207. tL.bindTooltip(_this.formatHMS([options.bounds._northEast.lng*3600,ii]).lng,{
  208. permanent:true,
  209. offset:[0,90]
  210. }).openTooltip();
  211. if(!L.Browser.ie){
  212. _this._map.on('move',function(){
  213. this.setLatLng(L.latLng(_this._map.getBounds()._northEast.lat,this.getLatLng().lng));
  214. },tL);
  215. }
  216. }
  217. }
  218. },
  219. _hideSTLayer:function(){
  220. var _this = this;
  221. _this._STLayerGroup.clearLayers();
  222. _this._map.removeLayer(_this._STLayerGroup);
  223. _this._STLayerTextGroup.clearLayers();
  224. _this._map.removeLayer(_this._STLayerTextGroup);
  225. }
  226. });
  227. L.control.STLatLngLayer = function(options){
  228. return new L.Control.STLatLngLayer(options);
  229. };