ZoomBox.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Control.js
  7. * @requires OpenLayers/Handler/Box.js
  8. */
  9. /**
  10. * Class: OpenLayers.Control.ZoomBox
  11. * The ZoomBox control enables zooming directly to a given extent, by drawing
  12. * a box on the map. The box is drawn by holding down shift, whilst dragging
  13. * the mouse.
  14. *
  15. * Inherits from:
  16. * - <OpenLayers.Control>
  17. */
  18. OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, {
  19. /**
  20. * Property: type
  21. * {OpenLayers.Control.TYPE}
  22. */
  23. type: OpenLayers.Control.TYPE_TOOL,
  24. /**
  25. * Property: out
  26. * {Boolean} Should the control be used for zooming out?
  27. */
  28. out: false,
  29. /**
  30. * Property: alwaysZoom
  31. * {Boolean} Always zoom in/out, when box drawed
  32. */
  33. alwaysZoom: false,
  34. /**
  35. * Method: draw
  36. */
  37. draw: function() {
  38. this.handler = new OpenLayers.Handler.Box( this,
  39. {done: this.zoomBox}, {keyMask: this.keyMask} );
  40. },
  41. /**
  42. * Method: zoomBox
  43. *
  44. * Parameters:
  45. * position - {<OpenLayers.Bounds>} or {<OpenLayers.Pixel>}
  46. */
  47. zoomBox: function (position) {
  48. if (position instanceof OpenLayers.Bounds) {
  49. var bounds;
  50. if (!this.out) {
  51. var minXY = this.map.getLonLatFromPixel(
  52. new OpenLayers.Pixel(position.left, position.bottom));
  53. var maxXY = this.map.getLonLatFromPixel(
  54. new OpenLayers.Pixel(position.right, position.top));
  55. bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat,
  56. maxXY.lon, maxXY.lat);
  57. } else {
  58. var pixWidth = Math.abs(position.right-position.left);
  59. var pixHeight = Math.abs(position.top-position.bottom);
  60. var zoomFactor = Math.min((this.map.size.h / pixHeight),
  61. (this.map.size.w / pixWidth));
  62. var extent = this.map.getExtent();
  63. var center = this.map.getLonLatFromPixel(
  64. position.getCenterPixel());
  65. var xmin = center.lon - (extent.getWidth()/2)*zoomFactor;
  66. var xmax = center.lon + (extent.getWidth()/2)*zoomFactor;
  67. var ymin = center.lat - (extent.getHeight()/2)*zoomFactor;
  68. var ymax = center.lat + (extent.getHeight()/2)*zoomFactor;
  69. bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax);
  70. }
  71. // always zoom in/out
  72. var lastZoom = this.map.getZoom();
  73. this.map.zoomToExtent(bounds);
  74. if (lastZoom == this.map.getZoom() && this.alwaysZoom == true){
  75. this.map.zoomTo(lastZoom + (this.out ? -1 : 1));
  76. }
  77. } else { // it's a pixel
  78. if (!this.out) {
  79. this.map.setCenter(this.map.getLonLatFromPixel(position),
  80. this.map.getZoom() + 1);
  81. } else {
  82. this.map.setCenter(this.map.getLonLatFromPixel(position),
  83. this.map.getZoom() - 1);
  84. }
  85. }
  86. },
  87. CLASS_NAME: "OpenLayers.Control.ZoomBox"
  88. });