谷歌地图GeoJson造型

时间:2014-10-06 22:17:13

标签: css google-maps 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"
            }
        }
    ]
}

我希望可以在单个多边形上设置样式,而不是整个数据。

1 个答案:

答案 0 :(得分: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
    };
});
相关问题