ZoomBox.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/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. * APIProperty: keyMask
  31. * {Integer} Zoom only occurs if the keyMask matches the combination of
  32. * keys down. Use bitwise operators and one or more of the
  33. * <OpenLayers.Handler> constants to construct a keyMask. Leave null if
  34. * not used mask. Default is null.
  35. */
  36. keyMask: null,
  37. /**
  38. * APIProperty: alwaysZoom
  39. * {Boolean} Always zoom in/out when box drawn, even if the zoom level does
  40. * not change.
  41. */
  42. alwaysZoom: false,
  43. /**
  44. * APIProperty: zoomOnClick
  45. * {Boolean} Should we zoom when no box was dragged, i.e. the user only
  46. * clicked? Default is true.
  47. */
  48. zoomOnClick: true,
  49. /**
  50. * Method: draw
  51. */
  52. draw: function() {
  53. this.handler = new OpenLayers.Handler.Box( this,
  54. {done: this.zoomBox}, {keyMask: this.keyMask} );
  55. },
  56. /**
  57. * Method: zoomBox
  58. *
  59. * Parameters:
  60. * position - {<OpenLayers.Bounds>} or {<OpenLayers.Pixel>}
  61. */
  62. zoomBox: function (position) {
  63. if (position instanceof OpenLayers.Bounds) {
  64. var bounds,
  65. targetCenterPx = position.getCenterPixel();
  66. if (!this.out) {
  67. var minXY = this.map.getLonLatFromPixel({
  68. x: position.left,
  69. y: position.bottom
  70. });
  71. var maxXY = this.map.getLonLatFromPixel({
  72. x: position.right,
  73. y: position.top
  74. });
  75. bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat,
  76. maxXY.lon, maxXY.lat);
  77. } else {
  78. var pixWidth = position.right - position.left;
  79. var pixHeight = position.bottom - position.top;
  80. var zoomFactor = Math.min((this.map.size.h / pixHeight),
  81. (this.map.size.w / pixWidth));
  82. var extent = this.map.getExtent();
  83. var center = this.map.getLonLatFromPixel(targetCenterPx);
  84. var xmin = center.lon - (extent.getWidth()/2)*zoomFactor;
  85. var xmax = center.lon + (extent.getWidth()/2)*zoomFactor;
  86. var ymin = center.lat - (extent.getHeight()/2)*zoomFactor;
  87. var ymax = center.lat + (extent.getHeight()/2)*zoomFactor;
  88. bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax);
  89. }
  90. // always zoom in/out
  91. var lastZoom = this.map.getZoom(),
  92. size = this.map.getSize(),
  93. centerPx = {x: size.w / 2, y: size.h / 2},
  94. zoom = this.map.getZoomForExtent(bounds),
  95. oldRes = this.map.getResolution(),
  96. newRes = this.map.getResolutionForZoom(zoom);
  97. if (oldRes == newRes) {
  98. this.map.setCenter(this.map.getLonLatFromPixel(targetCenterPx));
  99. } else {
  100. var zoomOriginPx = {
  101. x: (oldRes * targetCenterPx.x - newRes * centerPx.x) /
  102. (oldRes - newRes),
  103. y: (oldRes * targetCenterPx.y - newRes * centerPx.y) /
  104. (oldRes - newRes)
  105. };
  106. this.map.zoomTo(zoom, zoomOriginPx);
  107. }
  108. if (lastZoom == this.map.getZoom() && this.alwaysZoom == true){
  109. this.map.zoomTo(lastZoom + (this.out ? -1 : 1));
  110. }
  111. } else if (this.zoomOnClick) { // it's a pixel
  112. if (!this.out) {
  113. this.map.zoomTo(this.map.getZoom() + 1, position);
  114. } else {
  115. this.map.zoomTo(this.map.getZoom() - 1, position);
  116. }
  117. }
  118. },
  119. CLASS_NAME: "OpenLayers.Control.ZoomBox"
  120. });