How to toggle the colour of picked polygons on a Fusion Tables layer

一世执手 提交于 2019-12-13 04:45:16

问题


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 :

  1. toggle the status of a polygon
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!