Google Maps: Center map and automatic zoom on single address

Snippet to get an automatic zoom level for a geocoded address.
map = new GMap2(document.getElementById("map_canvas"));
geocoder = new GClientGeocoder();
function showAddress(address) {
  geocoder.getLocations(address, function (locations) {
  var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
  var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
  var east = locations.Placemark[0].ExtendedData.LatLonBox.east;
  var west = locations.Placemark[0].ExtendedData.LatLonBox.west;
  var bounds = new GLatLngBounds(new GLatLng(south, west),
  new GLatLng(north, east));
  var point = new GLatLng(locations.Placemark[0].Point.coordinates[1], locations.Placemark[0].Point.coordinates[0])
  map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
  // Placing a marker in the center of the map
  var marker = new GMarker(point);
  map.addOverlay(marker);
});
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: