PanZoom.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Control.js
  7. * @requires OpenLayers/Events/buttonclick.js
  8. */
  9. /**
  10. * Class: OpenLayers.Control.PanZoom
  11. * The PanZoom is a visible control, composed of a
  12. * <OpenLayers.Control.PanPanel> and a <OpenLayers.Control.ZoomPanel>. By
  13. * default it is drawn in the upper left corner of the map.
  14. *
  15. * Inherits from:
  16. * - <OpenLayers.Control>
  17. */
  18. OpenLayers.Control.PanZoom = OpenLayers.Class(OpenLayers.Control, {
  19. /**
  20. * APIProperty: slideFactor
  21. * {Integer} Number of pixels by which we'll pan the map in any direction
  22. * on clicking the arrow buttons. If you want to pan by some ratio
  23. * of the map dimensions, use <slideRatio> instead.
  24. */
  25. slideFactor: 50,
  26. /**
  27. * APIProperty: slideRatio
  28. * {Number} The fraction of map width/height by which we'll pan the map
  29. * on clicking the arrow buttons. Default is null. If set, will
  30. * override <slideFactor>. E.g. if slideRatio is .5, then the Pan Up
  31. * button will pan up half the map height.
  32. */
  33. slideRatio: null,
  34. /**
  35. * Property: buttons
  36. * {Array(DOMElement)} Array of Button Divs
  37. */
  38. buttons: null,
  39. /**
  40. * Property: position
  41. * {<OpenLayers.Pixel>}
  42. */
  43. position: null,
  44. /**
  45. * Constructor: OpenLayers.Control.PanZoom
  46. *
  47. * Parameters:
  48. * options - {Object}
  49. */
  50. initialize: function(options) {
  51. this.position = new OpenLayers.Pixel(OpenLayers.Control.PanZoom.X,
  52. OpenLayers.Control.PanZoom.Y);
  53. OpenLayers.Control.prototype.initialize.apply(this, arguments);
  54. },
  55. /**
  56. * APIMethod: destroy
  57. */
  58. destroy: function() {
  59. if (this.map) {
  60. this.map.events.unregister("buttonclick", this, this.onButtonClick);
  61. }
  62. this.removeButtons();
  63. this.buttons = null;
  64. this.position = null;
  65. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  66. },
  67. /**
  68. * Method: setMap
  69. *
  70. * Properties:
  71. * map - {<OpenLayers.Map>}
  72. */
  73. setMap: function(map) {
  74. OpenLayers.Control.prototype.setMap.apply(this, arguments);
  75. this.map.events.register("buttonclick", this, this.onButtonClick);
  76. },
  77. /**
  78. * Method: draw
  79. *
  80. * Parameters:
  81. * px - {<OpenLayers.Pixel>}
  82. *
  83. * Returns:
  84. * {DOMElement} A reference to the container div for the PanZoom control.
  85. */
  86. draw: function(px) {
  87. // initialize our internal div
  88. OpenLayers.Control.prototype.draw.apply(this, arguments);
  89. px = this.position;
  90. // place the controls
  91. this.buttons = [];
  92. var sz = {w: 18, h: 18};
  93. var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
  94. this._addButton("panup", "north-mini.png", centered, sz);
  95. px.y = centered.y+sz.h;
  96. this._addButton("panleft", "west-mini.png", px, sz);
  97. this._addButton("panright", "east-mini.png", px.add(sz.w, 0), sz);
  98. this._addButton("pandown", "south-mini.png",
  99. centered.add(0, sz.h*2), sz);
  100. this._addButton("zoomin", "zoom-plus-mini.png",
  101. centered.add(0, sz.h*3+5), sz);
  102. this._addButton("zoomworld", "zoom-world-mini.png",
  103. centered.add(0, sz.h*4+5), sz);
  104. this._addButton("zoomout", "zoom-minus-mini.png",
  105. centered.add(0, sz.h*5+5), sz);
  106. return this.div;
  107. },
  108. /**
  109. * Method: _addButton
  110. *
  111. * Parameters:
  112. * id - {String}
  113. * img - {String}
  114. * xy - {<OpenLayers.Pixel>}
  115. * sz - {<OpenLayers.Size>}
  116. *
  117. * Returns:
  118. * {DOMElement} A Div (an alphaImageDiv, to be precise) that contains the
  119. * image of the button, and has all the proper event handlers set.
  120. */
  121. _addButton:function(id, img, xy, sz) {
  122. var imgLocation = OpenLayers.Util.getImageLocation(img);
  123. var btn = OpenLayers.Util.createAlphaImageDiv(
  124. this.id + "_" + id,
  125. xy, sz, imgLocation, "absolute");
  126. btn.style.cursor = "pointer";
  127. //we want to add the outer div
  128. this.div.appendChild(btn);
  129. btn.action = id;
  130. btn.className = "olButton";
  131. //we want to remember/reference the outer div
  132. this.buttons.push(btn);
  133. return btn;
  134. },
  135. /**
  136. * Method: _removeButton
  137. *
  138. * Parameters:
  139. * btn - {Object}
  140. */
  141. _removeButton: function(btn) {
  142. this.div.removeChild(btn);
  143. OpenLayers.Util.removeItem(this.buttons, btn);
  144. },
  145. /**
  146. * Method: removeButtons
  147. */
  148. removeButtons: function() {
  149. for(var i=this.buttons.length-1; i>=0; --i) {
  150. this._removeButton(this.buttons[i]);
  151. }
  152. },
  153. /**
  154. * Method: onButtonClick
  155. *
  156. * Parameters:
  157. * evt - {Event}
  158. */
  159. onButtonClick: function(evt) {
  160. var btn = evt.buttonElement;
  161. switch (btn.action) {
  162. case "panup":
  163. this.map.pan(0, -this.getSlideFactor("h"));
  164. break;
  165. case "pandown":
  166. this.map.pan(0, this.getSlideFactor("h"));
  167. break;
  168. case "panleft":
  169. this.map.pan(-this.getSlideFactor("w"), 0);
  170. break;
  171. case "panright":
  172. this.map.pan(this.getSlideFactor("w"), 0);
  173. break;
  174. case "zoomin":
  175. this.map.zoomIn();
  176. break;
  177. case "zoomout":
  178. this.map.zoomOut();
  179. break;
  180. case "zoomworld":
  181. this.map.zoomToMaxExtent();
  182. break;
  183. }
  184. },
  185. /**
  186. * Method: getSlideFactor
  187. *
  188. * Parameters:
  189. * dim - {String} "w" or "h" (for width or height).
  190. *
  191. * Returns:
  192. * {Number} The slide factor for panning in the requested direction.
  193. */
  194. getSlideFactor: function(dim) {
  195. return this.slideRatio ?
  196. this.map.getSize()[dim] * this.slideRatio :
  197. this.slideFactor;
  198. },
  199. CLASS_NAME: "OpenLayers.Control.PanZoom"
  200. });
  201. /**
  202. * Constant: X
  203. * {Integer}
  204. */
  205. OpenLayers.Control.PanZoom.X = 4;
  206. /**
  207. * Constant: Y
  208. * {Integer}
  209. */
  210. OpenLayers.Control.PanZoom.Y = 4;