【问题标题】:LeafletJS, GeoJSON, selecting polygonsLeafletJS,GeoJSON,选择多边形
【发布时间】:2014-04-10 15:17:18
【问题描述】:

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

我的目的是更改选定多边形的颜色并为其设置动画。

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

【问题讨论】:

    标签: leaflet geojson


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 2020-07-23
      • 2017-03-15
      • 2022-12-22
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多