-1

I am using the following sample code to embed a Google Fusion Tables map:

function initialize() {
            var map = new google.maps.Map(document.getElementById('map-canvas'), {
                center: new google.maps.LatLng(37.4, -122.1),
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            var layer = new google.maps.FusionTablesLayer({
            query: {
                select: 'Address',
                from: // My table id,
            },
                map: map
            });
        }

        google.maps.event.addDomListener(window, 'load', initialize);

You can see from the above code that the "center" is set to a specific long and lat. If I remove the "center" property, it does not render the map at all.

I am looking at some way to automatically determine the best location to center in on. I believe using just general Google Maps that you can determine the boundaries of the location markers and center in on that? So maybe there is a way to get the list of locations from the table and then working out the boundaries from there?

Note: I understand there are other options for embedding the map, such as an iframe, but iframe is not an option for us in this project. Further, I understand that you can take the HTML/JS from the "Publish" option within the Fusion table, but the requirement is for the user to be able to embed the map with only the table id so they cannot copy/paste any HTML/JS.

Tim
  • 2,667
  • 4
  • 32
  • 39
  • What does your table look like? What kind of geographic data do you have in the columns? – geocodezip May 19 '14 at 03:31
  • Im just testing at this stage but its basically a list of addresses. I have set a "Location" field which I believe is geocoded – Tim May 19 '14 at 03:32
  • If fusion tables geocodes the points, you can't access those locations. Not much you can do to center and zoom the map, unless a "link to" functionality will work, but that would require the user to center the map. How many addresses? You might be able to use the google maps geocoder to get their positions. – geocodezip May 19 '14 at 03:40
  • Well the table doesnt necessarily have to geocode the points, I just did that as a part of testing. As for number of addresses, I would say that it could range from a handful to a few hundred at most although I doubt there would be many that were more than say fifty or so – Tim May 19 '14 at 03:46

1 Answers1

1

If you geocode the addresses externally and include the coordinates in the table, you can query the table using the Fusion Tables API v1 or GViz (the google visualization API), add the coordintates to a google.maps.LatLngBounds object, and use that to center and zoom the map to show all the points with google.maps.Map.fitBounds.

example (using GViz)

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • This is great. Could you explain how geocoding externally and including the coordinates in the table might work? From a simple users perspective, would they have to find the coordinates themselves and enter them into a column for each? – Tim May 19 '14 at 04:52
  • What do you mean by a "simple user"? Use an external geocoding service (google's is problematic, it has a requirement that you not store the data permanently, you must periodically refresh the coordinates). You might be able to show them a map with the marker placed using the google geocoder and let them drag it to the correct location (IANAL, read the terms of service for the geocoder). – geocodezip May 19 '14 at 04:57