Skip to content

Instantly share code, notes, and snippets.

@tristen
Created November 17, 2014 17:15
Show Gist options
  • Select an option

  • Save tristen/8847da524eb42148aa28 to your computer and use it in GitHub Desktop.

Select an option

Save tristen/8847da524eb42148aa28 to your computer and use it in GitHub Desktop.
Filter markers from HTML generated markers
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Filter markers from HTML generated markers</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
#menu {
position:absolute;
top:20px;
right:20px;
z-index:1;
}
nav {
border:1px solid rgba(0,0,0,0.10);
margin-bottom:10px;
}
nav a {
display:block;
background-color:#fff;
padding:5px 10px;
border-bottom:1px solid rgba(0,0,0,0.10);
color:#404040;
}
nav a:hover { background-color:#eee; color:#202020; }
nav a:last-child { border-bottom:none; }
</style>
</head>
<body>
<div id='map'>
<section id='menu'>
<nav id='listings'>
<a href='#' class='listing' data-filter='east' data-lat='-79.39712047576904' data-lng='43.62669447164394'>Toronto</a>
<a href='#' class='listing' data-filter='mid' data-lat='-87.63072967529297' data-lng='41.874673839758'>Chicago</a>
<a href='#' class='listing' data-filter='west' data-lat='-123.37234497070311' data-lng='48.45106561953216'>Victoria</a>
<a href='#' class='listing' data-filter='west' data-lat='-124.44419860839844' data-lng='49.34883548204658'>Qualicum Beach</a>
<a href='#' class='listing' data-filter='west' data-lat='-125.8985137939453' data-lng='49.12691530777389'>Tofino</a>
</nav>
<nav id='filter'>
<a href='#' class='listing' data-filter='all'>All</a>
<a href='#' class='listing' data-filter='east'>East</a>
<a href='#' class='listing' data-filter='mid'>Mid</a>
<a href='#' class='listing' data-filter='west'>West</a>
</nav>
</section>
</div>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoiZmFsbHNlbW8yIiwiYSI6IjhsbHFBMkEifQ.OMXud5BW3OAF-_usSJjy0Q';
var map = L.mapbox.map('map', 'examples.map-20v6611k').setView([39, -97], 4);
var geojson = [];
var markers = L.mapbox.featureLayer().addTo(map);
var listings = document.getElementById('listings').querySelectorAll('.listing');
var filter = document.getElementById('filter').querySelectorAll('.listing');
Array.prototype.forEach.call(listings, function(l) {
// Build the markers
geojson.push({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [
l.getAttribute('data-lat'),
l.getAttribute('data-lng')
]
},
properties: {
'title': l.textContent,
'filter': l.getAttribute('data-filter'),
'marker-color': '#ff8888'
}
});
// Assign a click handler to each listing
l.addEventListener('click', function(ev) {
ev.preventDefault();
ev.stopPropagation();
markers.setFilter(function(m) {
// When these marker listings are
// selected, show them all.
return true;
});
markers.eachLayer(function(m) {
var props = m.feature.properties;
if (props.title === ev.target.textContent) {
m.openPopup();
}
});
});
});
console.log(geojson);
markers.setGeoJSON(geojson);
Array.prototype.forEach.call(filter, function(l) {
// Assign a click handler to each listing
l.addEventListener('click', function(ev) {
ev.preventDefault();
ev.stopPropagation();
markers.setFilter(function(m) {
var filter = l.getAttribute('data-filter');
if (filter === 'all') return true;
return (m.properties.filter === filter);
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment