Domanda

Following the Non-exclusive markers example here, https://www.mapbox.com/mapbox.js/example/v1.0.0/non-exclusive-markers/, It is quite easy to filter by one property at a time.

I need to write some JS that can take a series of feature properties and show or hide each marker depending on whether any of those properties match a given checkbox's value. Can anyone point me in the direction of a method that might accomplish this?

Check out the demo, http://picturethiswebcenter.com/ods_map/

My filter code,

//filter locations by category
var filters = document.getElementById('filters');
var cats = document.getElementsByClassName('filter');
var states = document.getElementsByClassName('filter2');

function change() { 
    // Find all checkboxes that are checked and build a list of their values
    var cats_on = [];
    for (var i = 0; i < cats.length; i++) {
        if (cats[i].checked) cats_on.push(cats[i].value);
    }

    var states_on = [];
    for (var i = 0; i < states.length; i++) {
        if (states[i].checked) states_on.push(states[i].value);
    }

    // The filter function takes a GeoJSON feature object
    // and returns true to show it or false to hide it.
    map.markerLayer.setFilter(function (f) {
        // check each property to see if its value is in the list
        // of categories that should be on, stored in the 'on' array
        return cats_on.indexOf(f.properties['category']) !== -1;
        return states_on.indexOf(f.properties['state']) !== -1;
    });

    return false;

}

// When the form is touched, re-filter markers
filters.onchange = change;
// Initially filter the markers
change;
È stato utile?

Soluzione

Some JavaScript tweaks:

return cats_on.indexOf(f.properties['category']) !== -1 ||
 states_on.indexOf(f.properties['state']) !== -1;

In JavaScript, || means 'or'.

change();

To call a function, you need to use ().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top