Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

javascript - Filter shows "true" instead of wanted labels in Leaflet

Filter certain numbers out to show as labels in Leaflet. The code below do filter correctly but it does not show the correct labels, it shows "true" on the map instead of the wanted numbers 650 to 699. What changes is needed to display the properties labels instead of "true" from this filtration?

var local_points = new L.layerGroup();

    function local (feature){
        var filter = (feature.properties.local >= 650 && feature.properties.local < 699)
        return filter;
    };

var collisionlocal = L.LayerGroup.collision({margin:5});

$.getJSON("/geodata/localID_points.geojson", function(json) {

var points = L.geoJSON.collision(null, {
    pointToLayer: function(feature,latlng){
    label3s = String('<span class="textLabelclass3small">' + local(feature) + '</span>')
return new L.marker(latlng, {
        icon:createLabelIcon("textLabelclass3small",label3s)
        });
        }
    });

    var createLabelIcon = function(labelClass,labelText){
        return L.divIcon({ 
            className: labelClass,
            html: labelText
            });
        };

points.addData(json);
collisionlocal.addLayer(points);
collisionlocal.addTo(local_points);

});

question from:https://stackoverflow.com/questions/66054354/filter-shows-true-instead-of-wanted-labels-in-leaflet

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to apply the filter to the geoJson call and not in the text:

var points = L.geoJSON.collision(null, {
    filter: local,
    pointToLayer: function(feature,latlng){
        label3s = String('<span class="textLabelclass3small">' + feature.properties.local + '</span>')
        return new L.marker(latlng, {
           icon:createLabelIcon("textLabelclass3small",label3s)
        });
     }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...