Google Maps API Range Limiting

© 2006, 2009 Mike Williams

Translate this page:


Limiting the range

If you want to limit the range of zoom levels that the user is allowed to use on your map, you can overwrite the .getMinimumResolution() and .getMaximumResolution() methods of all the map types.

If you want to limit the range of movement that the user is allowed to use on your map, you can add listeners for the "move" event, and countermand any operations that go outside the permitted region.

Things happen so quickly that there's no time for anything out of bounds to get plotted, so it all looks reasonably smooth.

Here's an example

What I've done for the position is to specify an allowable region in GLatLngBounds() format, and require that the centre point lie within that region.

Potential Pitfalls

  1. Make sure that your permitted region is larger than the visible region at the maximum permitted zoom level.
  2. Allow enough room around the edge so that the automatic movements caused by opening infowindows partially off screen are not restricted.
  3. Don't call map.setCenter() unless you are actually changing the values. E.g. if you omit the "if" statement around "map.centerAtLatLng(new GPoint(X,Y));", then that operation itself would trigger a "move" event, and the code would go into a loop.
  4. Don't try to use the .lat.hi. .lat.lo, .lng.hi and .lng.lo properties of the GLatLngBounds() object.
    The values are in radians, not degrees, so they can't be used for recentering the view.

Back to Mike's Homepage
Up to the start of this tutorial