Alfonse
Alfonse

Reputation: 11

Setting maximum and minimum bounds using Google Maps API

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?

Upvotes: 1

Views: 3991

Answers (2)

Daniel Vassallo
Daniel Vassallo

Reputation: 344291

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(); });

Upvotes: 2

Alfonse
Alfonse

Reputation: 1

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

Upvotes: 0

Related Questions