[GIS] Zooming Google Maps to fit the extents of a Fusion Table layer

extentsgoogle mapsgoogle-fusion-tables

How can I zoom a Google Maps control to fit the contents of a Fusion Table layer?
The layer only contains pushpin / marker point data.

One method is to perform a REST query on the Fusion Table and to calculate the bounds, and zoom to these (i.e. as illustrated here: http://gmaps-samples.googlecode.com/svn/trunk/fusiontables/fitbounds.html ). However this seems very inefficient – especially for larger datasets, because all of the table data has to be sent to the client and compared.

This question appears to ask the same query, but the answer does not apply. The answer simply queries the data for the current map extents. Nice and efficient, but the opposite of what I want. I want to zoom to display all available data.

Best Answer

You can get your all your table data and find the bounds from the response:

      var bounds = new google.maps.LatLngBounds();
  for(i = 0; i < numRows; i++) {
      var point = new google.maps.LatLng(
          parseFloat(response.getDataTable().getValue(i, 0)),
          parseFloat(response.getDataTable().getValue(i, 1)));
      bounds.extend(point);
  }
  // zoom to the bounds
  map.fitBounds(bounds);
}

This comes from a good example:

http://www.geocodezip.com/www_vciregionmap_comC.html and view source