Last active
August 29, 2015 14:21
-
-
Save iriberri/9472cd1e5a09476a3c59 to your computer and use it in GitHub Desktop.
Change map according to selector
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> | |
<title>Search example | CartoDB.js</title> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> | |
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" /> | |
<style> | |
html, body, #map { | |
height: 95%; | |
padding: 0; | |
margin: 0; | |
} | |
</style> | |
<script src="http://code.jquery.com/jquery-latest.min.js"></script> | |
<link rel="stylesheet" | |
href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" /> | |
<!--[if lte IE 8]> | |
<link rel="stylesheet" | |
href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.ie.css" /> | |
<![endif]--> | |
</head> | |
<body> | |
<div id="map"></div> | |
<div> | |
<p>Use the selector to choose the country in which you want the map to be centered:</p> | |
<select id="trail-selector"> | |
<option value="esp">Spain</option> | |
<option value="por">Portugal</option> | |
<option value="ita">Italy</option> | |
<option value="bra">Brazil</option> | |
</select> | |
</div> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script> | |
<script> | |
var layer; | |
function main() { | |
var map; | |
var layerUrl = 'https://team.cartodb.com/u/iriberri/api/v2/viz/431ca5d0-02f5-11e5-9e34-0e4fddd5de28/viz.json'; | |
cartodb.createVis('map', layerUrl) | |
.on('done', function(vis, layer) { | |
map = vis.getNativeMap(); | |
}).on('error', function() { | |
cartodb.log.log("some error occurred"); | |
}); | |
function processFormData() { | |
var input = $( "#trail-selector option:selected" ).text(); | |
return input; | |
} | |
var search_text; | |
$(document).on('change','#trail-selector',function(){ | |
search_text = processFormData(); | |
var sql = new cartodb.SQL({ user: 'iriberri' }); | |
sql.getBounds("SELECT * FROM world_borders_9 where name Like '" + search_text + | |
"'").done(function(bounds) { | |
map.fitBounds(bounds) | |
}); | |
}); | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment