0

According to @Mourner in this gihub issue regarding filtering cluster in Mapbox here.

"you'll have to filter manually and run setData to reset the whole dataset..."

I have also found an example of this in this fiddle.

But how do I set a filter in the .setData method?

My html:

<li>
    <a href="#" class="filter" alt="Arrive">
        <i class="fas fa-ship"></i>
        <span class="">Arrive</span>
    </a>
</li>

my js:

$(".filter").click(function () {
  var filter_key = $(this).attr("alt");
  var filter = ["==", 'navtag', filter_key];
  map.getSource('points').setData(**filtered data**);
..
});
3

I believe he means that you should run a filter function over the data you use in setData. So if your data is an array of points with properties, you must first eliminate those points that do not meet your navtag criteria (in this case). This may involve using setData several times if your condition is dynamic.

This will probably look something like:

data.features = data.features.filter(feature => feature.navtag === 'foo')
map.getSource('points').setData(data)
  • Thanks @Richard Law. It sounds like setData expects a geojson? Can I see an example of filtering the geojson before passing filtered dataset to setData? – wtgeographer Mar 19 '18 at 16:02
  • Added an example. – Richard Law Mar 19 '18 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.