Guest User

Patch minZoom/maxZoom

a guest
Sep 21st, 2012
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //--------------------------------------------------------------------------------------------------
  2. //  $Id: OpenLayers_Map_minZoom_maxZoom_Patch.js,v 1.2 2012/02/25 11:54:36 wolf Exp wolf $
  3. //--------------------------------------------------------------------------------------------------
  4. //
  5. //  class:  OpenLayers.Map
  6. //  fields: minZoom, maxZoom
  7. //  methods: moveTo, getNumZoomLevels
  8. //
  9. //--------------------------------------------------------------------------------
  10. //
  11. //  The Problem:
  12. //
  13. //  Map zoom needs to be restricted to a certain range.
  14. //
  15. //--------------------------------------------------------------------------------
  16. //
  17. //  The solution (not really):
  18. //
  19. //  The method Map.moveTo is patched to limit its parameter "zoom" to the
  20. //  range defined by the Map fields "minZoom" and "maxZoom".
  21. //
  22. //--------------------------------------------------------------------------------
  23. //
  24. //  BEWARE!
  25. //
  26. //  The OpenLayers zoom/pan logic is based on the assumption, that zero is
  27. //  allways a safe value for zoom. THIS PATCH SUBVERTS THIS ASSUMPTION!
  28. //
  29. //  There may be side effects.
  30. //
  31. //--------------------------------------------------------------------------------
  32. //  http://www.netzwolf.info/kartografie/openlayers/restrictedzoom.htm
  33. //--------------------------------------------------------------------------------
  34.  
  35. (function () {
  36.     OpenLayers.Map.prototype.minZoom = null;
  37.     OpenLayers.Map.prototype.maxZoom = null;
  38.  
  39.     var moveTo = OpenLayers.Map.prototype.moveTo;
  40.  
  41.     OpenLayers.Map.prototype.moveTo = function (lonlat, zoom, options) {
  42.  
  43.         if (this.minZoom && zoom<this.minZoom) {
  44.             zoom  = this.minZoom;
  45.             center = ol.map.getCenter();
  46.             if (center) lonlat=center;
  47.         }
  48.         if (this.maxZoom && zoom>this.maxZoom) {
  49.             zoom = this.maxZoom;
  50.             center = ol.map.getCenter();
  51.             if (center) lonlat=center;
  52.         }
  53.  
  54.         return moveTo.apply (this, [lonlat, zoom, options]);
  55.     };
  56.  
  57.     var getNumZoomLevels = OpenLayers.Map.prototype.getNumZoomLevels;
  58.  
  59.     OpenLayers.Map.prototype.getNumZoomLevels = function () {
  60.  
  61.         var num = getNumZoomLevels.apply (this, arguments);
  62.         if (this.maxZoom && num>this.maxZoom+1) num = this.maxZoom+1;
  63.         return num;
  64.     };
  65. })();
  66.  
  67. //--------------------------------------------------------------------------------------------------
  68. //  $Id: OpenLayers_Map_minZoom_maxZoom_Patch.js,v 1.2 2012/02/25 11:54:36 wolf Exp wolf $
  69. //--------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment