tags:

views:

899

answers:

2

Hi all,

Was wondering if someone could provide an example of how to set maximum and minimum extents using GMaps2? In OpenLayers, here's how it is done:

var point1 = new OpenLayers.Geometry.Point(7, 48);
point1.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); 

var point2 = new OpenLayers.Geometry.Point(11, 54);
point2.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); 

var bounds = new OpenLayers.Bounds();
bounds.extend(point1);
bounds.extend(point2);
bounds.toBBOX(); 

map = new OpenLayers.Map("map", {
   maxExtent: bounds, 
   maxResolution:"auto", 
   maxZoomLevel: 8, 
   projection:"EPSG:900913",
   controls: []  
});
map.addLayer(new OpenLayers.Layer.OSM.Osmarender("Osmarender"));

map.zoomToMaxExtent();
//map.zoomToExtent(bounds);

What would be the GMaps2 equivalent? Any help would greatly be appreciated.

Al

+1  A: 

The following is the Google Maps API 2.x equivalent to limit the panning of the map to predefined bounds:

// Bounds for North America
var allowedBounds = new GLatLngBounds(new GLatLng(48.197218, -127.529297), 
                                      new GLatLng(28.72913, -68.818359));

function checkBounds() { 
    if (allowedBounds.contains(map.getCenter())) {
        return;
    }

    var c = map.getCenter();
    var x = c.lng();
    var y = c.lat();
    var maxX = allowedBounds.getNorthEast().lng();
    var maxY = allowedBounds.getNorthEast().lat();
    var minX = allowedBounds.getSouthWest().lng();
    var minY = allowedBounds.getSouthWest().lat();

    if (x < minX) {x = minX;}
    if (x > maxX) {x = maxX;}
    if (y < minY) {y = minY;}
    if (y > maxY) {y = maxY;}

    map.setCenter(new GLatLng(y, x));
}

GEvent.addListener(map, "move", function() { checkBounds(); });
Daniel Vassallo
A: 

Thanks for getting back to me Chris and Daniel. What I meant by 'extents' is that when zoomed out is maximized, the world map doesn't tile. I don't want to see 2, 3 or even 4 North America's and other continents. The world is big enough with just one. When I zoom in, I want to set a limit to how far one can zoom in to account for our data resolution. Zoom in too far returns results outside our data resolution. I did manage to find an acceptable solution by restricting the zoom levels. Anyway, FWIW, here's what I came up. It's a bit less verbose but isn't doing exactly what Daniel does in his code:

  //===== Restrict Zoom Levels =====     
  var mapTypes = map.getMapTypes();
  // Overwrite the getMinimumResolution() and getMaximumResolution() methods
  for (var i=0; i<mapTypes.length; i++) {
    mapTypes[i].getMinimumResolution = function() {return 2;}
    mapTypes[i].getMaximumResolution = function() {return 9;}
  }

I still want to try Daniel's code to see what it does. Special thanks to you for taking the time to write this up Dan.

-=Al

Alfonse
Alfonse, I'm glad you found your solution. The code that I posted does not limit the zooming, but the panning left/right, top/bottom. Basically with the coordinates that I used, it does not let you move away from North America. You will not be able to drag the map over Europe, for example. Obviously you can use any coordinates. The constraints that you are using for the zooming are actually recommended to be used with my implementation.
Daniel Vassallo
Thanks Daniel.. I see what you're saying... tried to implement it as you suggest but was unsuccessful. There are other things going on in my code though and I haven't had time to troubleshoot it yet. Will let you know how it goes.
Alfonse