Friday, November 19, 2010

Fusion Tables

Spatial Queries

The spatial queries that were recently added to Fusion Tables mean that it is now very easy to create a very powerful Store Locator using just Google Maps and Fusion Tables. If you have a Fusion Table that contains your store data you can easily create a Google Map that will load the stores within a defined area or load the nearest stores to a given location.

Using the bounding box spatial query you can load points within a predefined area. You can also use the 'distance' query to load the stores nearest to a given point.

Load Nearest Points

We can use the getCenter function to get the latitude and longitude at the centre of the map. It is then possible to load the nearest points of interest in our Fusion Table to this point. The following function will load the ten nearest points to the current centre of the map.

function loadNearest() {
layer.setQuery("SELECT Country FROM 188044 ORDER BY ST_DISTANCE(Country, LATLNG( " + map.getCenter().lat() +', '+ map.getCenter().lng() + ")) LIMIT 10");
}

We can then add a button to the map to call the function with

onclick="loadNearest()"

Bounding Box

It is now also possible to easily create a bounding box with Fusion Tables. Using a bounding box we can define an area on the map and load in all the points from a Fusion Table that fall within that area.

function boundingBox() {
layer.setQuery("SELECT Country FROM 188044 WHERE ST_INTERSECTS(Country, RECTANGLE(LATLNG(35.77, -12.57), LATLNG(66.6, 37.3)))");
}

We can add a button to the map to call the bounding box with:

onclick="boundingBox()"

This Spatial Queries demo shows the bounding box in action. To add the 'ditance' query just add the code I have outlined above.

________________

No comments: