Last active
February 6, 2016 12:15
-
-
Save ernesmb/a9337c0eb2fe5848963f to your computer and use it in GitHub Desktop.
Actions on Feature Click
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>Actions on Feature Click | 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" /> | |
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" /> | |
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script> | |
<style> | |
html, body { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
position:relative; | |
} | |
#map { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
#info{ | |
font-size: 20px; | |
color:#000; | |
background-color: rgba(255,255,255,0.8); | |
width:128px; | |
height:72px; | |
top:15px; | |
right:15px; | |
border:1px solid black; | |
position: absolute; | |
z-index: 11; | |
padding: 5px; | |
text-align: center; | |
display: table-cell; | |
vertical-align: center; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<div id="info"></div> | |
<script> | |
function main() { | |
var sublayer | |
var map = L.map('map', { | |
zoomControl: true, | |
center: [40.418709, -3.703277], | |
zoom: 3 | |
}); | |
// add a nice baselayer from Stamen | |
L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', { | |
attribution: 'Stamen and CartoDB attribution' | |
}).addTo(map); | |
cartodb.createLayer(map, { | |
user_name: 'ernestomb', | |
type: 'cartodb', | |
sublayers: [{ | |
sql: 'SELECT * FROM ne_10m_populated_places_simple_7_copy', | |
cartocss:'#ne_10m_populated_places_simple_7_copy{ marker-fill-opacity: 0.9; marker-line-color: #FFF; marker-line-width: 0; marker-line-opacity: 1; marker-placement: point; marker-multi-policy: largest; marker-type: ellipse; marker-fill: #FFCC00; marker-allow-overlap: true; marker-clip: false; marker-comp-op: multiply;}', | |
interactivity:"cartodb_id, pop_max, name" | |
}] | |
},{https:true}) | |
.addTo(map) | |
.on('done', function(layer) { | |
sublayer = layer.getSubLayer(0); | |
sublayer.setInteraction(true); | |
sublayer.on('featureClick', function(event, latlng, pos, data){ | |
console.log(data.pop_max); | |
//add here any action you want to perform on click | |
population=data.pop_max; | |
name=data.name; | |
info.innerHTML='Population in <b>'+name+'</b> is: <b>'+population+'</b>'; | |
}); | |
}); | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment