-
-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94353b1
commit 79ea3d6
Showing
1 changed file
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
--- | ||
layout: example | ||
category: example | ||
title: Filter markers | ||
description: Filter a set of markers based on a property value in the data. | ||
--- | ||
<style> | ||
.filter-group { | ||
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif; | ||
font-weight: 600; | ||
position: absolute; | ||
top: 10px; | ||
right: 10px; | ||
z-index: 1; | ||
border-radius: 3px; | ||
width: 120px; | ||
color: #fff; | ||
} | ||
|
||
.filter-group input[type=checkbox]:first-child + label { | ||
border-radius: 3px 3px 0 0; | ||
} | ||
|
||
.filter-group label:last-child { | ||
border-radius: 0 0 3px 3px; | ||
border: none; | ||
} | ||
|
||
.filter-group input[type=checkbox] { | ||
display: none; | ||
} | ||
|
||
.filter-group input[type=checkbox] + label { | ||
background-color: #3386c0; | ||
display: block; | ||
cursor: pointer; | ||
padding: 10px; | ||
border-bottom: 1px solid rgba(0, 0, 0, 0.25); | ||
} | ||
|
||
.filter-group input[type=checkbox] + label { | ||
background-color: #3386c0; | ||
text-transform: capitalize; | ||
} | ||
|
||
.filter-group input[type=checkbox] + label:hover, | ||
.filter-group input[type=checkbox]:checked + label { | ||
background-color: #4ea0da; | ||
} | ||
|
||
.filter-group input[type=checkbox]:checked + label:before { | ||
content: '✔'; | ||
margin-right: 5px; | ||
} | ||
</style> | ||
<div id='map'></div> | ||
<nav id='filter-group' class='filter-group'></nav> | ||
|
||
<script> | ||
var markers = { | ||
"type": "FeatureCollection", | ||
"features": [{ | ||
"type": "Feature", | ||
"properties": { | ||
"description": "<div class=\"marker-title\">Make it Mount Pleasant</div><p><a href=\"http://www.mtpleasantdc.com/makeitmtpleasant\" target=\"_blank\" title=\"Opens in a new window\">Make it Mount Pleasant</a> is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>", | ||
"id": "krhy93as", | ||
"marker-symbol": "theatre" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-77.038659, 38.931567] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": { | ||
"description": "<div class=\"marker-title\">Mad Men Season Five Finale Watch Party</div><p>Head to Lounge 201 (201 Massachusetts Avenue NE) Sunday for a <a href=\"http://madmens5finale.eventbrite.com/\" target=\"_blank\" title=\"Opens in a new window\">Mad Men Season Five Finale Watch Party</a>, complete with 60s costume contest, Mad Men trivia, and retro food and drink. 8:00-11:00 p.m. $10 general admission, $20 admission and two hour open bar.</p>", | ||
"marker-symbol": "theatre" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-77.003168, 38.894651] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": { | ||
"description": "<div class=\"marker-title\">Big Backyard Beach Bash and Wine Fest</div><p>EatBar (2761 Washington Boulevard Arlington VA) is throwing a <a href=\"http://tallulaeatbar.ticketleap.com/2012beachblanket/\" target=\"_blank\" title=\"Opens in a new window\">Big Backyard Beach Bash and Wine Fest</a> on Saturday, serving up conch fritters, fish tacos and crab sliders, and Red Apron hot dogs. 12:00-3:00 p.m. $25.grill hot dogs.</p>", | ||
"marker-symbol": "bar" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-77.090372, 38.881189] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": { | ||
"description": "<div class=\"marker-title\">Seersucker Bike Ride and Social</div><p>Feeling dandy? Get fancy, grab your bike, and take part in this year's <a href=\"http://dandiesandquaintrelles.com/2012/04/the-seersucker-social-is-set-for-june-9th-save-the-date-and-start-planning-your-look/\" target=\"_blank\" title=\"Opens in a new window\">Seersucker Social</a> bike ride from Dandies and Quaintrelles. After the ride enjoy a lawn party at Hillwood with jazz, cocktails, paper hat-making, and more. 11:00-7:00 p.m.</p>", | ||
"marker-symbol": "bicycle" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-77.052477, 38.943951] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": { | ||
"description": "<div class=\"marker-title\">Muhsinah</div><p>Jazz-influenced hip hop artist <a href=\"http://www.muhsinah.com\" target=\"_blank\" title=\"Opens in a new window\">Muhsinah</a> plays the <a href=\"http://www.blackcatdc.com\">Black Cat</a> (1811 14th Street NW) tonight with <a href=\"http://www.exitclov.com\" target=\"_blank\" title=\"Opens in a new window\">Exit Clov</a> and <a href=\"http://godsilla.bandcamp.com\" target=\"_blank\" title=\"Opens in a new window\">Gods’illa</a>. 9:00 p.m. $12.</p>", | ||
"marker-symbol": "music" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-77.031706, 38.914581] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": { | ||
"description": "<div class=\"marker-title\">A Little Night Music</div><p>The Arlington Players' production of Stephen Sondheim's <a href=\"http://www.thearlingtonplayers.org/drupal-6.20/node/4661/show\" target=\"_blank\" title=\"Opens in a new window\"><em>A Little Night Music</em></a> comes to the Kogod Cradle at The Mead Center for American Theater (1101 6th Street SW) this weekend and next. 8:00 p.m.</p>", | ||
"marker-symbol": "music" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-77.020945, 38.878241] | ||
} | ||
}, { | ||
"type": "Feature", | ||
"properties": { | ||
"description": "<div class=\"marker-title\">Truckeroo</div><p><a href=\"http://www.truckeroodc.com/www/\" target=\"_blank\">Truckeroo</a> brings dozens of food trucks, live music, and games to half and M Street SE (across from Navy Yard Metro Station) today from 11:00 a.m. to 11:00 p.m.</p>", | ||
"marker-symbol": "music" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-77.007481, 38.876516] | ||
} | ||
}] | ||
}; | ||
|
||
var filterGroup = document.getElementById('filter-group'); | ||
var map = new mapboxgl.Map({ | ||
container: 'map', | ||
style: 'mapbox://styles/mapbox/streets-v8', | ||
center: [-77.04, 38.907], | ||
zoom: 11.15 | ||
}); | ||
|
||
map.on('load', function() { | ||
// Add marker data as a new GeoJSON source. | ||
map.addSource("markers", { | ||
"type": "geojson", | ||
"data": markers | ||
}); | ||
|
||
markers.features.forEach(function(feature) { | ||
var symbol = feature.properties['marker-symbol']; | ||
var layerID = 'poi-' + symbol; | ||
|
||
// Add a layer for this symbol type if it hasn't been added already. | ||
if (!map.getLayer(layerID)) { | ||
map.addLayer({ | ||
"id": layerID, | ||
"type": "symbol", | ||
"source": "markers", | ||
"layout": { | ||
"icon-image": symbol + "-15", | ||
"icon-allow-overlap": true | ||
}, | ||
"filter": ["==", "marker-symbol", symbol] | ||
}); | ||
|
||
// Add checkbox and label elements for the layer. | ||
var input = document.createElement('input'); | ||
input.type = 'checkbox'; | ||
input.id = layerID; | ||
input.checked = true; | ||
filterGroup.appendChild(input); | ||
|
||
var label = document.createElement('label'); | ||
label.setAttribute('for', layerID); | ||
label.textContent = symbol; | ||
filterGroup.appendChild(label); | ||
|
||
// When the checkbox changes, update the visibility of the layer. | ||
input.addEventListener('change', function(e) { | ||
map.setLayoutProperty(layerID, 'visibility', | ||
e.target.checked ? 'visible' : 'none'); | ||
}); | ||
} | ||
}); | ||
}); | ||
</script> |