LeafletJS,GeoJSON,选择多边形

时间:2014-04-10 15:17:19

标签: leaflet geojson

我有一个显示土耳其地区的GeoJSON文件。我正在使用Leaflet添加多边形作为GeoJSON图层,我需要一种通过id或其他东西选择多边形的方法(我将使用jQuery)。

我的目的是更改和动画所选多边形的颜色。

我知道我可以循环使用GeoJSON并在循环时设置不同的样式,但我需要一种更动态的方式。

1 个答案:

答案 0 :(得分:1)

好的,我找到了一种方法,但不确定这是否是最佳方式......

var polygons = {};

addRegionalLayer();

function addRegionalLayer(){
    cityjsonLayer = L.geoJson(turkeyadm_districts, {
        onEachFeature: onEachFeature,
        style: function(feature) {
            return {
                fillColor: 'red',
                weight: 1,
                opacity: 1,
                color: 'white',
                dashArray: '3',
                fillOpacity: 0.7
            };
        }
    }).addTo(map);
}


function onEachFeature(feature, layer){

    // I am adding the polygon layer to the polygons dictionary with the districts name
    polygons[feature.properties.NAME_2] = layer;

}



// this is test function ---> i am setting the color of the polygon that has key name //'Kadiköy' with random colors
setInterval(function() {
    var back = ["#ff0000","blue","yellow"];
    var rand = back[Math.floor(Math.random() * back.length)];
    polygons['Kadiköy'].setStyle({
        fillColor:rand,
        opacity:1
    });
}, 40);
相关问题