I have a functioning map using Google's Fusion Tables and Maps API V3 here. Everything works with exception to 3 of my polygons which I will fix when I figure this next step out. I was asked if i can have each checkbox selection zoom to the polygon in question. I have tried using the geoXml.parseKmlString()
method and Google's Visualization API to accomplish this request as suggested in many other posts with no luck. I'm still relatively new to JavaScript and completely new to the Google's geoxml3 KML processor and Visualization API.
I have looked at various examples to try and figure this out and the closest i could find is this:
I could not get it to work with my code.
Does anyone have any suggestions on how I can incorporate the code from the Assembly Map into my code to work with the functioning code I already have in place?
Thanks for your time.
My current code is as follows:
var map;
var catchSD8 = new google.maps.LatLng(50.0267110, -116.907234);
var tableId = '1yc4wo1kBGNJwpDm6e-eJY_KL1YhQWfftjhA38w8';
var varID = [
"Adam Robertson Elementary",
"Blewett Elementary",
"Brent Kennedy",
"Canyon-Lister Elementary",
"Crawford Bay Elementary/Secondary",
"Erickson Elementary",
"Hume Elementary",
"JV Humphries Elementary",
"JV Humphries Secondary",
"Jewett Elementary",
"Kootenay Lake",
"Lower Kootenay Band",
"LV Rogers Secondary",
"Mount Sentinel Middle",
"Mount Sentinel Secondary",
"Prince Charles Secondary",
"Redfish Elementary",
"Rosemont Elementary",
"Salmo Elementary",
"Salmo Secondary",
"Trafalgar Middle School",
"W.E. Graham Community Elementary",
"W.E. Graham Community Secondary",
"Winlaw Elementary",
"Yahk Elementary"
];
function initialize() {
var myOptions = new google.maps.Map(document.getElementById('map_canvas'), {
center: catchSD8,
zoom: 8,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
scaleControl: true,
scaleControlOptions: {
position: google.maps.ControlPosition.BOTTOM_LEFT
},
mapTypeId: google.maps.MapTypeId.ROADMAP
});
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var layer = new google.maps.FusionTablesLayer();
filterMap(layer, tableId, map);
var mySchool;
var limit = varID.length;
for(var i=0; i < limit; i++){
mySchool = document.getElementById(varID[i]);
(function(schl,lyr,tbl,mp){
google.maps.event.addDomListener(schl,'click', function() {
filterMap(lyr,tbl,mp);
});
}(mySchool,layer,tableId,map));
}
}
// Filter the map based on checkbox selection.
function filterMap(layer, tableId, map) {
var where = generateWhere();
if (where) {
if (!layer.getMap()) {
layer.setMap(map);
}
layer.setOptions({
query: {
select: 'Location',
from: tableId,
where: where
}
});
} else {
layer.setMap(null);
}
}
// Generate a where clause from the checkboxes. If no boxes
// are checked, return an empty string.
function generateWhere() {
var filter = [];
var schools = document.getElementsByName('school');
for (var i = 0, school; school = schools[i]; i++) {
if (school.checked) {
var schoolName = school.value.replace(/'/g, '\\\'');
filter.push("'" + schoolName + "'");
}
}
var where = '';
if (filter.length) {
where = "School IN (" + filter.join(',') + ')';
}
return where;
}
google.maps.event.addDomListener(window, 'load', initialize);
You first need to load the used libraries:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://geoxml3.googlecode.com/svn/branches/polys/geoxml3.js"></script>
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart', 'table', 'geomap']});
</script>
Also copy the functions zoom2query()
and zoomTo()
into your document.
Inside your function filterMap() call zoomToQuery() with the query as argument:
function filterMap(layer, tableId, map) {
var where = generateWhere();
if (where) {
zoom2query('SELECT Location from '+tableId+ ' where '+where);
//continue with your code