Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Created December 14, 2012 03:09
Show Gist options
  • Save wboykinm/4282388 to your computer and use it in GitHub Desktop.
Save wboykinm/4282388 to your computer and use it in GitHub Desktop.
Omigodomigodomigod - The guts of a fully-functional layer switcher build onto mapbox.js - via the good folks at MotherJones
//Establish URLs and names for all layers
var layer_names = {
//Base layer
'tasneemraja.map-7lrzv81u':'base',
//Overlays
'tasneemraja.election-2012-voter-regi':'Tougher Registration Laws',
'tasneemraja.election-2012-voter-id':'Voter ID Required',
'tasneemraja.felons':'Disenfranchised Felons',
'tasneemraja.election-2012-early-absentee':'Early and Absentee Restrictions',
'tasneemraja.counties-election-results-2008':'2008 Election Results'
}
// var marker_colors = {
// 'white':'#fff',
// 'black':'#000',
// 'blue':'#73b5e5',
}
for (var i = 0; i < possible_layers.length; i++) {
var n = possible_layers[i];
var item = document.createElement('li');
var layer = document.createElement('a');
layer.href = '#';
layer.id = n;
layer.className = '';
layer.innerHTML = layer_names[n];
layer.onclick = function(e) {
e.preventDefault();
e.stopPropagation();
var listitems = document.getElementsByClassName('active');
for (var i = 0; i < listitems.length; i++) {
listitems[i].className = '';
};
mapbox.load(this.id, function(o) {
var layers = map.getLayers();
for (var i = 0; i < layers.length; i++) {
for (var j = 0; j < possible_layers.length; j++) {
if (layers[i].name === possible_layers[j]) {
map.removeLayer(layers[i].name);
}
}
}
removeLegend();
map.addTileLayer(o.layer);
addLegend(o.legend);
map.interaction.auto();
});
this.className = 'active';
};
item.appendChild(layer);
layers.appendChild(item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment