javascript - Mapbox: How to filter markers by category? -
i creating first map using mapbox , want able filter markers category. following example on mapbox doc page:
https://www.mapbox.com/mapbox.js/example/v1.0.0/multiple-marker-filters/
i have property each marker following key: cat_code. can't seem legend appear can toggle markers off , on.
anyone have clues?
l.mapbox.accesstoken = 'pk.eyj1ijoizg9zcyisimeioii1nfituws4in0.-9qpbofe3jc68wduqa1akg'; var map = l.mapbox.map('map', 'mapbox.light'); var featurelayer = { "type": "featurecollection", "features": [ { "type": "feature", "properties": { "marker-color": "#7e7e7e", "marker-size": "medium", "marker-symbol": "heliport", "cat_code": "12311" }, "geometry": { "type": "point", "coordinates": [ 46.40625, 53.54030739150022 ] } }, { "type": "feature", "properties": { "marker-color": "#7e7e7e", "marker-size": "medium", "marker-symbol": "", "cat_code": "1231566" }, "geometry": { "type": "point", "coordinates": [ 58.00781249999999, 55.97379820507658 ] } }, { "type": "feature", "properties": { "marker-color": "#7e7e7e", "marker-size": "medium", "marker-symbol": "", "cat_code": "12311" }, "geometry": { "type": "point", "coordinates": [ 45.3515625, 62.431074232920906 ] } } ] }; // find , store variable reference list of filters. var filters = document.getelementbyid('filters'); // wait until marker layer loaded in order build list of possible // types. if doing featurelayer, should change // map.featurelayer variable have assigned featurelayer. map.featurelayer.on('ready', function() { // collect types of symbols in layer. can // hardcode array of types if know want filter on, // var types = ['foo', 'bar']; var typesobj = {}, types = []; var features = map.featurelayer.getgeojson().features; (var = 0; < features.length; i++) { typesobj[features[i].properties['cat_code']] = true; } (var k in typesobj) types.push(k); var checkboxes = []; // create filter interface. (var = 0; < types.length; i++) { // create an input checkbox , label inside. var item = filters.appendchild(document.createelement('div')); var checkbox = item.appendchild(document.createelement('input')); var label = item.appendchild(document.createelement('label')); checkbox.type = 'checkbox'; checkbox.id = types[i]; checkbox.checked = true; // create label right of checkbox explanatory text label.innerhtml = types[i]; label.setattribute('for', types[i]); // whenever person clicks on checkbox, call update(). checkbox.addeventlistener('change', update); checkboxes.push(checkbox); } // function called whenever clicks on checkbox , changes // selection of markers displayed. function update() { var enabled = {}; // run through each checkbox , record whether checked. if is, // add object of types display, otherwise not. (var = 0; < checkboxes.length; i++) { if (checkboxes[i].checked) enabled[checkboxes[i].id] = true; } map.pois.setfilter(function(feature) { // if symbol in list, return true. if not, return false. // 'in' operator in javascript that: given string // or number, says if in object. // 2 in { 2: true } // true // 2 in { } // false return (feature.properties['cat_code'] in enabled); }); } });
you never loading geojson onto map's featurelayer
, leaving in original form object. did renaming , fixed in fiddle.
Comments
Post a Comment