Skip to content

Commit e7ebb10

Browse files
committed
✨ Add Filter for institution name
1 parent fe421e5 commit e7ebb10

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
</span>
4141
</div>
4242
<button id="reset">Reset Zoom</button>
43+
<div class="filter-ctrl">
44+
<input id="filter-input" type="text" name="filter" placeholder="Filter Institution by Name" />
45+
</div>
4346
<script src="./mapbox.js"></script>
4447
<!-- Google Analytics -->
4548
<script>

mapbox.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var initial = [ -60.643775, -32.954626]
55
var zoom = 1.5
66
var url = 'https://raw.githubusercontent.com/ELC/jupyter-map/master/jupyter-map.geojson';
77
var places; // Get value async below
8+
var filterInput = document.getElementById('filter-input');
89

910
var map = new mapboxgl.Map({
1011
container: 'map',
@@ -43,8 +44,56 @@ async function addData() {
4344
}
4445
});
4546

47+
// Filter by Input
48+
49+
let layerIDs = [];
50+
51+
places.features.forEach(function(feature) {
52+
var symbol = feature.properties['institution_name'];
53+
var layerID = symbol.trim().toLowerCase();
54+
55+
// Add a layer for this symbol type if it hasn't been added already.
56+
if (!map.getLayer(layerID)) {
57+
map.addLayer({
58+
"id": layerID,
59+
"type": "symbol",
60+
"source": "data",
61+
"layout": {
62+
"icon-image": "marker-15",
63+
"icon-allow-overlap": true,
64+
"icon-size": 1.5,
65+
"text-field": "",
66+
'text-allow-overlap': true,
67+
'text-size': 14,
68+
"text-letter-spacing": 0.05,
69+
"text-offset": [0, 2],
70+
},
71+
"paint": {
72+
"text-color": "#202",
73+
"text-halo-color": "#fff",
74+
"text-halo-width": 2
75+
},
76+
"filter": ["==", "institution_name", symbol]
77+
});
78+
79+
layerIDs.push(layerID);
80+
}
4681
});
4782

83+
filterInput.addEventListener('keyup', function(e) {
84+
// If the input value matches a layerID set
85+
// it's visibility to 'visible' or else hide it.
86+
map.setLayoutProperty('places', "visibility",
87+
!e.target.value ? "visible" : "none");
88+
89+
var value = e.target.value.trim().toLowerCase();
90+
layerIDs.forEach(function(layerID) {
91+
map.setLayoutProperty(layerID, 'visibility',
92+
layerID.includes(value) ? 'visible' : 'none');
93+
map.setLayoutProperty(layerID, 'text-field',
94+
(e.target.value && layerID.includes(value)) ? '{institution_name}' : '');
95+
});
96+
});
4897
}
4998

5099
// First time
@@ -153,6 +202,9 @@ map.on('mouseleave', 'places', function(e) {
153202
});
154203

155204
map.on('click', function(e) {
205+
filterInput.value = ""
206+
let event = new Event('keyup');
207+
filterInput.dispatchEvent(event)
156208
map.getCanvas().style.cursor = '';
157209
});
158210

style.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,25 @@ a {
4949
.mapboxgl-popup {
5050
max-width: 300px;
5151
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
52+
}
53+
54+
.filter-ctrl {
55+
position: absolute;
56+
top: 120px;
57+
right: 12px;
58+
z-index: 1;
59+
width: 180px;
60+
}
61+
62+
.filter-ctrl input[type=text] {
63+
font: 12px/20px "Helvetica Neue", Arial, Helvetica, sans-serif;
64+
width: 100%;
65+
border: 0;
66+
background-color: #fff;
67+
height: 40px;
68+
margin: 0;
69+
color: rgba(0,0,0,.5);
70+
padding: 10px;
71+
box-shadow: 0 0 0 2px rgba(0,0,0,0.1);
72+
border-radius: 3px;
5273
}

0 commit comments

Comments
 (0)