Skip to content

Instantly share code, notes, and snippets.

@tristen
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save tristen/63deab17e6ebc799e95c to your computer and use it in GitHub Desktop.

Select an option

Save tristen/63deab17e6ebc799e95c to your computer and use it in GitHub Desktop.
Build markers from HTML
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Build markers from HTML</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%; }
#listings {
position:absolute;
top:20px;
right:20px;
z-index:1;
border:1px solid rgba(0,0,0,0.10);
}
#listings a {
display:block;
background-color:#fff;
padding:5px 10px;
border-bottom:1px solid rgba(0,0,0,0.10);
color:#404040;
}
#listings a:hover { background-color:#eee; color:#202020; }
#listings a:last-child { border-bottom:none; }
</style>
</head>
<body>
<div id='map'>
<nav id='listings'>
<a href='#' class='listing' data-lat='-79.39712047576904' data-lng='43.62669447164394'>Toronto</a>
<a href='#' class='listing' data-lat='-87.63072967529297' data-lng='41.874673839758'>Chicago</a>
<a href='#' class='listing' data-lat='-123.37234497070311' data-lng='48.45106561953216'>Victoria</a>
<a href='#' class='listing' data-lat='-124.44419860839844' data-lng='49.34883548204658'>Qualicum Beach</a>
<a href='#' class='listing' data-lat='-125.8985137939453' data-lng='49.12691530777389'>Tofino</a>
</nav>
</div>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoiZmFsbHNlbW8yIiwiYSI6IjhsbHFBMkEifQ.OMXud5BW3OAF-_usSJjy0Q';
var map = L.mapbox.map('map', 'examples.map-20v6611k')
.setView([39, -97], 4);
var listings = document.getElementById('listings').querySelectorAll('.listing');
Array.prototype.forEach.call(listings, function(l) {
// Build the markers
var marker = L.marker(new L.LatLng(l.getAttribute('data-lng'), l.getAttribute('data-lat')), {
icon: L.mapbox.marker.icon({
'marker-color': '#ff8888'
})
}).bindPopup(l.textContent).addTo(map);
// Assign a click handler to each listing
l.addEventListener('click', function(ev) {
ev.preventDefault();
ev.stopPropagation();
map.setView(marker.getLatLng(), 12);
marker.openPopup();
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment