First thing’s first: you’re going to need some included files.
Wait, what was that last one?! It was fusiontips. The link there goes to the Github repo of the forked code from Google Code to facilitate events on FT layers. To use that code, just go grab it and include it.
After that it’s fairly straight forward to setup your Google Map part.
Up until here it’s all pretty easy. We’re establishing some stuff, declaring stuff, and using my Googled best center of the US lat/long coordinates. Now we’ll add the Fusion Table layer the old fashioned way, and import some styling that was set on that layer in the Fusion Tables UI:
Note that I set suppressInfoWindows: true there. this is so the default tool-tips don’t come up when you click on the map. I want supreme customization of how the data is interacted with, and Google’s default actions won’t cut it. In this particular case I want to be able to have more than one value for an area on the map, and the areas are defined by a zip code. The table I’m using includes the geometry for every zip code in the US, and it contains data about what companies operate in that zip code. More than one company can operate in a zip code at any one time, so we’re using the Fusion Table layer to define the geometry through Google Maps API, but we’re going to use a separate call to the table to get the info about what’s in that geometry. We’re going to use a click to generate that separate call, and we’re using the previously mentioned fusiontips to listen for that event. In the example from fusiontips you can use a mouseover event too, but I wanted a click. I’ll leave in the mouseover code in a comment to show it can be used that way too.
You’ll see there that we added an event listener defined by the layer which extended enableMapTips from fusiontips. This is really the key to all of it. This code means that the fEvent argument passed from the click or hover event contains the information from the layer that we described in that layer.enableMapTips definition. In this case I’m passing only the ZIPCODE so that I can do a secondary search on the ZIPCODE later.
You may notice that at this point we’re kinda querying the same data set 3 times to get this to work. There may be a more efficient way to do this, but I wasn’t able to get it working cleaner as of this writing. We have an initial map building query to define the territories to be drawn. This is being done via the Google Maps API, and it is done to show the areas we mean to display. The second query defines the clickable or hover areas. This is simply done to give us control over what happens with the onclick event on an area. The third interaction is to get specific data, even where more than one result exists, and display it. That looks like this:
In the step before this one there was a call to getInfo(). This bit of code shows that getInfo is a $.get on the same data set. This is so we can limit our result set, and work with more than one return. By default on a Fusion Table layer Google will only show the top result on an area. This is the main problem we’re overcoming with all of this: the ability to control an event and show more than one item in a result set.
The callback on getInfo points to formatInfo and all that does is take the response from the GET request to Fusion Tables and iterates over the results, outputting each line item into an li, and then dumps that HTML into a div in the body of your page called info. You can do whatever you want with that div from there.
And there you have it! A way to work with a multiline result set from a Fusion Table query in Google Maps! Here’s a full code example: