Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Forked from tmcw/index.html
Created December 18, 2012 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wboykinm/4331911 to your computer and use it in GitHub Desktop.
Save wboykinm/4331911 to your computer and use it in GitHub Desktop.
Mapbox.js layer switcher using map.setLayer
<!DOCTYPE html>
<html>
<head>
<script src='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'></script>
<script src='http://code.jquery.com/jquery-1.8.3.min.js'></script>
<link href='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
#switch { position:absolute; top:0; bottom:0; left: 300px width:300px; }
</style>
</head>
<body>
<div id='map'></div>
<div id='switch'>
<button data-layer='0'>a</button>
<button data-layer='1'>b</button>
<button data-layer='2'>c</button>
</div>
<script>
mapbox.load(['landplanner.map-a0wbwf43', 'landplanner.blackmarble2', 'landplanner.enstrat_dni_101111b'], function(o) {
var map = mapbox.map('map').zoom(3).center({lat: 40, lon: -90});
map.addLayer(o[0].layer);
$('button').click(function() {
map.setLayerAt(0, o[$(this).data('layer')].layer);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment