Leaflet.js: is it possible to filter geoJSON features by property?

夙愿已清 提交于 2020-01-03 13:31:07

问题


I'm looking around and I see a lot of information about how to show/hide layers. That's cool, but since I can add arbitrary properties to geoJSON features I kind of expect to be able to filter them accordingly.

For instance if I have features 1, 2 & 3 with these properties

  1. small | red | sweet
  2. large | green | sour
  3. small | red | hot

How would I filter them by size? Or by color or flavor?


回答1:


http://leafletjs.com/examples/geojson.html

Yes you can, just add a filter function like:

L.geoJson(someFeatures, {
    filter: function(feature, layer) {
        return feature.properties.show_on_map;
    }
}).addTo(map);

Or if you want dynamic updating, there's a great answer in this other SO question: Leaflet: Update GeoJson filter?




回答2:


I have added the plugin for filtering markers by tags at Leaflet.tagFilterButton.

If you add tags option to your markers you can filter them by your tags/categories. For example:

L.geoJson(jsonObject, {
    pointToLayer: function(feature, latlng) {
        L.marker(latlng, {
            tags: ['small', 'red', 'sweet']
        });
    }
}).addTo( map );

L.control.tagFilterButton({
    data: ['small', 'red', 'sweet']
}).addTo( map );


来源:https://stackoverflow.com/questions/21974597/leaflet-js-is-it-possible-to-filter-geojson-features-by-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!