2014-10-06 87 views
1

我想在每個多邊形上設置樣式,特別是填充顏色。 我知道我能做到這一點:谷歌地圖GeoJson造型

map.data.loadGeoJson('http://localhost:8080/my.json'); 
map.data.setStyle({  
    fillColor: 'red' 
}); 

但我想設置單獨填補多邊形的顏色。我嘗試以下this answer on GIS Stack設置,但它不工作:

{ 
    "type": "FeatureCollection", 
    "features": [ 
     { 
      "type": "Feature", 
      "properties": {     
      }, 
      "geometry": { 
       "type": "Polygon", 
       "coordinates": [ 
        [ 
        [ 
         -80.4545, 
         43.5061 
        ] 
       ] 
       ] 
      }, 
      "style":{ 
       "fill":"red", 
       "stroke-weight": "1", 
       "stroke-width":"3", 
       "fill-opacity":"0.6" 
      } 
     } 
    ] 
} 

我希望它可以設置個人多邊形的造型,而不是整個數據。

回答

2

原來這是可以使用function分配方式:

// Color Capital letters blue, and lower case letters red. 
// Capital letters are represented in ascii by values less than 91 
map.data.setStyle(function(feature) { 
    var ascii = feature.getProperty('ascii'); 
    var color = ascii > 91 ? 'red' : 'blue'; 
    return { 
     fillColor: color, 
     strokeWeight: 1 
    }; 
}); 
+1

這幫助我,謝謝。 – mapr 2015-03-26 21:10:14