|
| 1 | +--- |
| 2 | +layout: example |
| 3 | +category: example |
| 4 | +title: Filter markers by text input |
| 5 | +description: Filter markers by icon name by typing in a text input. |
| 6 | +--- |
| 7 | +<style> |
| 8 | + .filter-ctrl { |
| 9 | + position: absolute; |
| 10 | + top: 10px; |
| 11 | + right: 10px; |
| 12 | + z-index: 1; |
| 13 | + width: 180px; |
| 14 | + } |
| 15 | + |
| 16 | + .filter-ctrl input[type=text] { |
| 17 | + font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif; |
| 18 | + width: 100%; |
| 19 | + border: 0; |
| 20 | + background-color: #fff; |
| 21 | + height: 40px; |
| 22 | + margin: 0; |
| 23 | + color: rgba(0,0,0,.5); |
| 24 | + padding: 10px; |
| 25 | + box-shadow: 0 0 0 2px rgba(0,0,0,0.1); |
| 26 | + border-radius: 3px; |
| 27 | + } |
| 28 | +</style> |
| 29 | +<div id='map'></div> |
| 30 | +<div class='filter-ctrl'> |
| 31 | + <input id='filter-input' type='text' name='filter' placeholder='Filter by marker name' /> |
| 32 | +</div> |
| 33 | + |
| 34 | +<script> |
| 35 | +var markers = { |
| 36 | + "type": "FeatureCollection", |
| 37 | + "features": [{ |
| 38 | + "type": "Feature", |
| 39 | + "properties": { |
| 40 | + "marker-symbol": "theatre" |
| 41 | + }, |
| 42 | + "geometry": { |
| 43 | + "type": "Point", |
| 44 | + "coordinates": [-77.038659, 38.931567] |
| 45 | + } |
| 46 | + }, { |
| 47 | + "type": "Feature", |
| 48 | + "properties": { |
| 49 | + "marker-symbol": "theatre" |
| 50 | + }, |
| 51 | + "geometry": { |
| 52 | + "type": "Point", |
| 53 | + "coordinates": [-77.003168, 38.894651] |
| 54 | + } |
| 55 | + }, { |
| 56 | + "type": "Feature", |
| 57 | + "properties": { |
| 58 | + "marker-symbol": "bar" |
| 59 | + }, |
| 60 | + "geometry": { |
| 61 | + "type": "Point", |
| 62 | + "coordinates": [-77.090372, 38.881189] |
| 63 | + } |
| 64 | + }, { |
| 65 | + "type": "Feature", |
| 66 | + "properties": { |
| 67 | + "marker-symbol": "bicycle" |
| 68 | + }, |
| 69 | + "geometry": { |
| 70 | + "type": "Point", |
| 71 | + "coordinates": [-77.052477, 38.943951] |
| 72 | + } |
| 73 | + }, { |
| 74 | + "type": "Feature", |
| 75 | + "properties": { |
| 76 | + "marker-symbol": "music" |
| 77 | + }, |
| 78 | + "geometry": { |
| 79 | + "type": "Point", |
| 80 | + "coordinates": [-77.031706, 38.914581] |
| 81 | + } |
| 82 | + }, { |
| 83 | + "type": "Feature", |
| 84 | + "properties": { |
| 85 | + "marker-symbol": "music" |
| 86 | + }, |
| 87 | + "geometry": { |
| 88 | + "type": "Point", |
| 89 | + "coordinates": [-77.020945, 38.878241] |
| 90 | + } |
| 91 | + }, { |
| 92 | + "type": "Feature", |
| 93 | + "properties": { |
| 94 | + "marker-symbol": "music" |
| 95 | + }, |
| 96 | + "geometry": { |
| 97 | + "type": "Point", |
| 98 | + "coordinates": [-77.007481, 38.876516] |
| 99 | + } |
| 100 | + }] |
| 101 | +}; |
| 102 | + |
| 103 | +var layerIDs = []; // Will contain a list used to filter against. |
| 104 | +var filterInput = document.getElementById('filter-input'); |
| 105 | +var map = new mapboxgl.Map({ |
| 106 | + container: 'map', |
| 107 | + style: 'mapbox://styles/mapbox/streets-v8', |
| 108 | + center: [-77.04, 38.907], |
| 109 | + zoom: 11.15 |
| 110 | +}); |
| 111 | + |
| 112 | +map.on('load', function() { |
| 113 | + // Add marker data as a new GeoJSON source. |
| 114 | + map.addSource('markers', { |
| 115 | + "type": "geojson", |
| 116 | + "data": markers |
| 117 | + }); |
| 118 | + |
| 119 | + markers.features.forEach(function(feature) { |
| 120 | + var symbol = feature.properties['marker-symbol']; |
| 121 | + var layerID = 'poi-' + symbol; |
| 122 | + |
| 123 | + // Add a layer for this symbol type if it hasn't been added already. |
| 124 | + if (!map.getLayer(layerID)) { |
| 125 | + map.addLayer({ |
| 126 | + "id": layerID, |
| 127 | + "type": "symbol", |
| 128 | + "source": "markers", |
| 129 | + "layout": { |
| 130 | + "icon-image": symbol + "-15", |
| 131 | + "icon-allow-overlap": true |
| 132 | + }, |
| 133 | + "filter": ["==", "marker-symbol", symbol] |
| 134 | + }); |
| 135 | + |
| 136 | + layerIDs.push(layerID); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + filterInput.addEventListener('keyup', function(e) { |
| 141 | + // If the input value matches a layerID set |
| 142 | + // it's visibility to 'visible' or else hide it. |
| 143 | + var value = e.target.value.trim().toLowerCase(); |
| 144 | + layerIDs.forEach(function(layerID) { |
| 145 | + map.setLayoutProperty(layerID, 'visibility', |
| 146 | + layerID.indexOf(value) > -1 ? 'visible' : 'none'); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
| 150 | +</script> |
0 commit comments