无法在传单L.geoJSON图层上设置样式

时间:2015-04-07 10:27:41

标签: leaflet geojson

我在基于L.geoJSON的图层上设置样式时遇到了一些麻烦,我的代码如下所示:

var addProperties = function addProperties(prop,map)
{
    //the API does not seem to support adding properties to an existing feature,
    //the idea here is simple:
    //(1) currentFeature.toGeoJson() needs to be called to obtain a json representation
    //(2) set the properties on the geojson
    //(3) create a new feature based on the geojson
    //(4) remove res and add the new feature as res

    var style = function style(feature){
        var markerStyle = {
            draggable: 'true', 
            icon: L.AwesomeMarkers.icon({
            icon: 'link', 
            prefix: 'glyphicon', 
            markerColor: 'red', 
            spin: true
           })
        };
        if(feature.geometry.type==='Point')
            return markerStyle;
    };

    var onEachFeature = function onEachFeature(feature,layer){
        console.log("Inside on each feature,checking to see if feature was passed to it ",feature);
        layer.on('click',function(e){
            //open display sidebar
            console.log("Checking to see if setupTabs exists ",setupTabs);
            setupTabs('#display-feature-tabs');
            console.log("Checking to see if featureInfo exists ",featureInfo);
            var featureInfoAPI =featureInfo('feature-properties');
            featureInfoAPI.swap(feature.properties);
             setTimeout(function() {
                    sidebar.show();
                }, 100);
        });
    };  

    console.log("Inside add properties");
    var geoJSON,feature;
    if(res != null)
    {
        geoJSON = res.toGeoJSON();
        geoJSON.properties = prop;
        console.log(geoJSON);
        feature = L.geoJson(geoJSON,{style:style,onEachFeature:onEachFeature});
        console.log("The new feature that has been created  ",feature);
        removeFeature(map);
        addFeature(feature);
        feature.addTo(map);

    }   
};

我也尝试了style方法,我希望为活动图层添加样式以获得点(也可以按类型为线条和折线添加样式)。

1 个答案:

答案 0 :(得分:0)

要设置点要素的样式,您应该使用pointToLayer选项。