Created
December 24, 2012 19:30
-
-
Save wboykinm/4370453 to your computer and use it in GitHub Desktop.
CartoDB SQL Switcher
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
html, body, #map { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
#layer_selector { | |
position: absolute; | |
top: 20px; | |
right: 20px; | |
padding: 0; | |
} | |
#layer_selector ul { | |
padding: 0; margin: 0; | |
list-style-type: none; | |
} | |
#layer_selector li { | |
border-bottom: 1px solid #999999; | |
padding: 15px 30px; | |
font-family: "Helvetica",Arial; | |
font-size: 13px; | |
color: #444; | |
cursor: auto; | |
} | |
#layer_selector li:hover { | |
background-color: #F0F0F0; | |
cursor: pointer; | |
} | |
#layer_selector li.selected { | |
background-color: #EEE; | |
} | |
</style> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v2/themes/css/cartodb.css" /> | |
<!--[if lte IE 8]> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v2/themes/css/cartodb.ie.css" /> | |
<![endif]--> | |
</head> | |
<body> | |
<div id="map"></div> | |
<div id="layer_selector" class="cartodb_infobox"> | |
<ul> | |
<li data="tout" class="selected">Tout</li> | |
<li data="ble">Ble</li> | |
<li data="fourr">Fourrage</li> | |
<li data="jache">Jacherre</li> | |
<li data="mais">Mais</li> | |
</ul> | |
</div> | |
</body> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v2/cartodb.js"></script> | |
<script> | |
// create layer selector | |
function createSelector(layer) { | |
var sql = new cartodb.SQL({ user: 'dai' }); | |
var $options = $('#layer_selector li'); | |
$options.click(function(e) { | |
// get the area of the selected layer | |
var $li = $(e.target); | |
var crop = $li.attr('data'); | |
// deselect all and select the clicked one | |
$options.removeClass('selected'); | |
$li.addClass('selected'); | |
// create query based on data from the layer | |
var query = "select * from ag_survey_2011"; | |
if(crop !== 'tout') { | |
query = "select * from ag_survey_2011 where " + crop + " > 0 "; | |
} | |
// change the query in the layer to update the map | |
layer.setQuery(query); | |
}); | |
} | |
function main() { | |
cartodb.createVis('map', 'http://dai.cartodb.com/api/v1/viz/11330/viz.json', { | |
tiles_loader: true, | |
center_lat: 34.88593094075317, | |
center_lon: -3.0322265625, | |
zoom: 8 | |
}) | |
.done(function(vis, layers) { | |
// layer 0 is the base layer, layer 1 is cartodb layer | |
createSelector(layers[1]) | |
}) | |
.error(function(err) { | |
console.log(err); | |
}); | |
} | |
window.onload = main; | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment