leaflet.Zoomslider.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. L.Control.Zoomslider = (function () {
  2. var Knob = L.Draggable.extend({
  3. initialize: function (element, stepHeight, knobHeight,minZ,maxZ,knobBgElement) {
  4. L.Draggable.prototype.initialize.call(this, element, element);
  5. this._element = element;
  6. this._minZ = minZ;
  7. this._maxZ = maxZ;
  8. this._stepHeight = stepHeight;
  9. this._knobHeight = knobHeight;
  10. this._knobBgElement = knobBgElement;
  11. this.on('predrag', function () {
  12. this._newPos.x = 0;
  13. this._newPos.y = this._adjust(this._newPos.y);
  14. }, this);
  15. },
  16. _adjust: function (y) {
  17. var value = Math.round(this._toValue(y));
  18. value = Math.max(0, Math.min(this._maxValue, value));
  19. this.setValueTxt(value + this._minZ);
  20. return this._toY(value);
  21. },
  22. // y = k*v + m
  23. _toY: function (value) {
  24. return this._k * value + this._m;
  25. },
  26. // v = (y - m) / k
  27. _toValue: function (y) {
  28. return (y - this._m) / this._k;
  29. },
  30. setSteps: function (steps) {
  31. var sliderHeight = steps * this._stepHeight;
  32. this._maxValue = steps - 1;
  33. // conversion parameters
  34. // the conversion is just a common linear function.
  35. this._k = -this._stepHeight;
  36. this._m = sliderHeight - (this._stepHeight + this._knobHeight) / 2;
  37. },
  38. setZoomScope:function(minZ,maxZ){
  39. this._minZ = minZ;
  40. this._maxZ = maxZ;
  41. },
  42. setPosition: function (y) {
  43. L.DomUtil.setPosition(this._element,
  44. L.point(0, this._adjust(y)));
  45. },
  46. setValue: function (v) {
  47. this.setPosition(this._toY(v));
  48. },
  49. getValue: function () {
  50. return this._toValue(L.DomUtil.getPosition(this._element).y);
  51. },
  52. //todo 仅标记**********== 修改的内容 ==**********
  53. /*S add by Song.Huang*/
  54. setValueTxt:function(t){
  55. this._element.innerHTML = t;
  56. this._knobBgElement.style.height = (this._maxZ - t)*this._stepHeight + 'px';
  57. }
  58. /*E add by Song.Huang*/
  59. });
  60. var Zoomslider = L.Control.extend({
  61. options: {
  62. position: 'topleft',
  63. // Height of zoom-slider.png in px
  64. stepHeight: 7,
  65. // Height of the knob div in px (including border)
  66. knobHeight: 13,
  67. styleNS: 'leaflet-control-zoomslider'
  68. },
  69. _overTimeOut:null,
  70. onAdd: function (map) {
  71. this._map = map;
  72. this._ui = this._createUI();
  73. this._knob = new Knob(this._ui.knob,
  74. this.options.stepHeight,
  75. this.options.knobHeight,map.getMinZoom(),map.getMaxZoom(),this._ui.knobBg);
  76. map .whenReady(this._initKnob, this)
  77. .whenReady(this._initEvents, this)
  78. .whenReady(this._updateSize, this)
  79. .whenReady(this._updateKnobValue, this)
  80. .whenReady(this._updateDisabled, this);
  81. return this._ui.bar;
  82. },
  83. onRemove: function (map) {
  84. map .off('zoomlevelschange', this._updateSize, this)
  85. .off('zoomend zoomlevelschange', this._updateKnobValue, this)
  86. .off('zoomend zoomlevelschange', this._updateDisabled, this);
  87. },
  88. _createUI: function () {
  89. var _this = this;
  90. var ui = {},
  91. ns = this.options.styleNS;
  92. ui.bar = L.DomUtil.create('div', ns + ' leaflet-bar'),
  93. ui.wrap = L.DomUtil.create('div', ns + '-wrap leaflet-bar-part', ui.bar),
  94. ui.zoomIn = this._createZoomBtn('in', 'top', ui.bar),
  95. ui.zoomOut = this._createZoomBtn('out', 'bottom', ui.bar),
  96. ui.body = L.DomUtil.create('div', ns + '-body', ui.wrap),
  97. ui.knobBg = L.DomUtil.create('div', ns + '-knob-bg'),
  98. ui.knob = L.DomUtil.create('div', ns + '-knob');
  99. L.DomEvent.disableClickPropagation(ui.bar);
  100. L.DomEvent.disableClickPropagation(ui.knob);
  101. L.DomEvent.on(ui.bar,'contextmenu',L.DomEvent.stopPropagation);
  102. //todo 仅标记**********== 修改的内容 ==**********
  103. /*S add by Song.Huang*/
  104. this._barJump = L.DomUtil.create('div','leaflet-bar-p-pic',ui.bar);
  105. this._barJump_country = L.DomUtil.create('span','bj_country', this._barJump);
  106. this._barJump_province = L.DomUtil.create('span','bj_province', this._barJump);
  107. this._barJump_city = L.DomUtil.create('span','bj_city', this._barJump);
  108. this._barJump_street = L.DomUtil.create('span','bj_street', this._barJump);
  109. L.DomEvent.on(ui.bar,"mouseover",function(){
  110. if(_this._overTimeOut !== null){
  111. clearTimeout(_this._overTimeOut);
  112. _this._overTimeOut = null;
  113. }
  114. L.DomUtil.addClass(ui.bar,'over');
  115. }).on(ui.bar,"mouseout",function(){
  116. clearTimeout(_this._overTimeOut);
  117. _this._overTimeOut = setTimeout(function() {
  118. L.DomUtil.removeClass(ui.bar, 'over');
  119. },100);
  120. });
  121. /*E add by Song.Huang*/
  122. return ui;
  123. },
  124. _createZoomBtn: function (zoomDir, end, container) {
  125. var classDef = this.options.styleNS + '-' + zoomDir
  126. + ' leaflet-bar-part'
  127. + ' leaflet-bar-part-' + end,
  128. link = L.DomUtil.create('a', classDef, container);
  129. //link.href = '#';
  130. switch (zoomDir){
  131. case 'out':
  132. link.title = "缩小";
  133. link.innerHTML = "-";
  134. break;
  135. case 'in':
  136. link.title = "放大";
  137. link.innerHTML = "+";
  138. }
  139. //link.title = 'Zoom ' + zoomDir;
  140. L.DomEvent.on(link, 'click', L.DomEvent.preventDefault);
  141. return link;
  142. },
  143. _initKnob: function () {
  144. this._knob.enable();
  145. //this._ui.body.appendChild(this._ui.knobBg);
  146. this._ui.body.appendChild(this._ui.knob);
  147. },
  148. _initEvents: function () {
  149. this._map
  150. .on('zoomlevelschange', this._updateSize, this)
  151. .on('zoomend zoomlevelschange', this._updateKnobValue, this)
  152. .on('zoomend zoomlevelschange', this._updateDisabled, this);
  153. //todo 仅标记**********== 修改的内容 ==**********
  154. /*S add by Song.Huang*/
  155. var _this = this;
  156. this._map.on('zoomstart',function(){
  157. L.DomUtil.addClass(this._ui.bar,'over');
  158. if(this.options.timeOut){
  159. clearTimeout(this.options.timeOut);
  160. this.options.timeOut = setTimeout(function(){
  161. L.DomUtil.removeClass(_this._ui.bar,'over');
  162. },2000);
  163. }else {
  164. this.options.timeOut = setTimeout(function(){
  165. L.DomUtil.removeClass(_this._ui.bar,'over');
  166. },2000);
  167. }
  168. },this);
  169. L.DomEvent.on(this._barJump_country,'click',function(){
  170. this._map.setZoom(3);
  171. },this);
  172. L.DomEvent.on(this._barJump_province,'click',function(){
  173. this._map.setZoom(7);
  174. },this);
  175. L.DomEvent.on(this._barJump_city,'click',function(){
  176. this._map.setZoom(11);
  177. },this);
  178. L.DomEvent.on(this._barJump_street,'click',function(){
  179. this._map.setZoom(17);
  180. },this);
  181. /*E add by Song.Huang*/
  182. //L.DomEvent.on(this._ui.body, 'click', this._onSliderClick, this);
  183. L.DomEvent.on(this._ui.zoomIn, 'click', this._zoomIn, this);
  184. L.DomEvent.on(this._ui.zoomOut, 'click', this._zoomOut, this);
  185. this._knob.on('dragend', this._updateMapZoom, this);
  186. //todo 仅标记**********== 修改的内容 ==**********
  187. /*S add by Song.Huang*/
  188. //this._knob.on('drag',this.,this);
  189. /*E add by Song.Huang*/
  190. },
  191. /*_onSliderClick: function (e) {
  192. var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e),
  193. y = L.DomEvent.getMousePosition(first).y
  194. - L.DomUtil.getViewportOffset(this._ui.body).y; // Cache this?
  195. this._knob.setPosition(y);
  196. this._updateMapZoom();
  197. },*/
  198. _zoomIn: function (e) {
  199. this._map.zoomIn(e.shiftKey ? 3 : 1);
  200. },
  201. _zoomOut: function (e) {
  202. this._map.zoomOut(e.shiftKey ? 3 : 1);
  203. },
  204. _zoomLevels: function () {
  205. var zoomLevels = this._map.getMaxZoom() - this._map.getMinZoom() + 1;
  206. return zoomLevels < Infinity ? zoomLevels : 0;
  207. },
  208. _toZoomLevel: function (value) {
  209. return value + this._map.getMinZoom();
  210. },
  211. _toValue: function (zoomLevel) {
  212. return zoomLevel - this._map.getMinZoom();
  213. },
  214. _updateSize: function () {
  215. this._knob.setZoomScope(this._map.getMinZoom(),this._map.getMaxZoom());
  216. var steps = this._zoomLevels();
  217. //this._ui.body.style.height = this.options.stepHeight * steps + 1+'px';
  218. //this._knob.setSteps(steps);
  219. if(this._map.getMaxZoom() != 19 || this._map.getMinZoom() != 1){
  220. this._barJump.style.visibility = 'visible';
  221. }else {
  222. this._barJump.style.visibility = 'visible';
  223. }
  224. },
  225. _updateMapZoom: function () {
  226. this._map.setZoom(this._toZoomLevel(this._knob.getValue()));
  227. },
  228. _updateKnobValue: function () {
  229. this._knob.setValue(this._toValue(this._map.getZoom()));
  230. //todo 仅标记**********== 修改的内容 ==**********
  231. /*S add by Song.Huang*/
  232. this._knob.setValueTxt(this._map.getZoom());
  233. /*E add by Song.Huang*/
  234. },
  235. _updateDisabled: function () {
  236. var zoomLevel = this._map.getZoom(),
  237. className = this.options.styleNS + '-disabled';
  238. L.DomUtil.removeClass(this._ui.zoomIn, className);
  239. L.DomUtil.removeClass(this._ui.zoomOut, className);
  240. if (zoomLevel === this._map.getMinZoom()) {
  241. L.DomUtil.addClass(this._ui.zoomOut, className);
  242. }
  243. if (zoomLevel === this._map.getMaxZoom()) {
  244. L.DomUtil.addClass(this._ui.zoomIn, className);
  245. }
  246. }
  247. });
  248. return Zoomslider;
  249. })();
  250. L.Map.mergeOptions({
  251. zoomControl: false,
  252. zoomsliderControl: false
  253. });
  254. L.Map.addInitHook(function () {
  255. if (this.options.zoomsliderControl) {
  256. this.zoomsliderControl = new L.Control.Zoomslider();
  257. this.addControl(this.zoomsliderControl);
  258. }
  259. });
  260. L.control.zoomslider = function (options) {
  261. return new L.Control.Zoomslider(options);
  262. };