Google Product Forums

Re: Constrain Google map to only show selection of global map

silverbackis Aug 10, 2012 11:31 AM
Posted in group: Google Fusion Tables

Categories: How do I...? :

I managed to do this the other day, saw someone else's post. Set the LatLng bounds then add a dragend listener so when a user stops dragging they are pulled back to your area. This was for England, not the whole of GB but just adjust the LatLng bounds. Here you go:
            map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
   var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(49.90878, -7.69042),
  new google.maps.LatLng(55.88770, 1.83496) 
  );
  //sw first
  //ne second
  // Listen for the dragend event
  google.maps.event.addListener(map, 'dragend', function() {
if (strictBounds.contains(map.getCenter())) return;
// We're out of bounds - Move the map back within the bounds
var c = map.getCenter(),
x = c.lng(),
y = c.lat(),
maxX = strictBounds.getNorthEast().lng(),
maxY = strictBounds.getNorthEast().lat(),
minX = strictBounds.getSouthWest().lng(),
minY = strictBounds.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 google.maps.LatLng(y, x));
  });