问题
I want to toggle polygon styles
Using the FT here: https://www.google.com/fusiontables/data?docid=1jgWYtlqGSPzlIa-is8wl1cZkVIWEm_89rWUwqFU
and the quick fusion table wizard http://fusion-tables-api-samples.googlecode.com/svn/trunk/FusionTablesLayerWizard/src/index.html
I thought it would be something like (where "Postcode district" is the column label in FT)
google.maps.event.addListener(layer_0, 'click', function(e) {
layer_0.set("styles", [{
where: "'Postcode district' = " + e.row['Postcode district'].value,
polygonOptions: {
fillColor: "#000000"
}
}]);
});
but that is just setting every single polygon to black.
Thanks.
回答1:
The value of Postcode disctrict
is a string, it has to be enclosed by single-quotes:
where: "'Postcode district' = '" + e.row['Postcode district'].value + "'",
Related to the additional question(preserve the highlighted status of the polygon until it will be clicked again):
You must store the status of the clicked polygon somewhere(e.g. in an object or array), then you'll be able to :
- toggle the status of a polygon
- create a collection of all "active" polygons and use this collection in the query for a
IN()
-condition
Sample:
//selected will be populated on layer-cllick with the postcode and
//a boolean (true when the area is highlighted, otherwise false)
selected={};
google.maps.event.addListener(layer_0, 'click', function(e) {
var val=e.row['Postcode district'].value,
vals=[];
//update the selected-object
selected[val]=(!selected[val])?true:false;
//populate the vals-array with the selected postcodes
for(var k in selected){
if(selected[k]){
vals.push(k);
}
}
layer_0.set("styles", [{
where: "'Postcode district' IN('"+vals.join("','")+"')",
polygonOptions: {
fillColor: "#000000"
}
}]);
});
Demo: http://jsfiddle.net/doktormolle/ZffgF/
来源:https://stackoverflow.com/questions/18738751/how-to-toggle-the-colour-of-picked-polygons-on-a-fusion-tables-layer