Skip to content

Instantly share code, notes, and snippets.

@michellechandra
Last active February 5, 2016 20:45
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 michellechandra/9c9356eeb6d5f8f30e34 to your computer and use it in GitHub Desktop.
Save michellechandra/9c9356eeb6d5f8f30e34 to your computer and use it in GitHub Desktop.
Infowindow with sublayer
<!DOCTYPE html>
<html>
<head>
<title>createLayer_sublayer + infowindow</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<!-- include cartodb css -->
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<!-- include cartodb.js library -->
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
<style type="text/css">
html,body,#map{
width: 100%;
height: 100%;
margin: 0;
}
</style>
</head>
<body>
<!-- define map object-->
<div id="map"></div>
<script>
// define map object
var map;
function main() {
//define map properties
map = L.map('map', {
zoomControl: true,
center: [-22.6285913, -42.9358045],
zoom: 3
});
// add OSM basemap to the map object
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'}).addTo(map);
var sublayers = [];
var style = [
'#world_borders{',
' polygon-fill: #1a9850;',
' polygon-opacity: 0.8;',
' line-color: #FFF;',
' line-width: 0.5;',
' line-opacity: 1;',
'}'
].join('\n')
cartodb.createLayer(map, {
user_name: 'documentation',
type: 'cartodb',
sublayers: [
{
sql: 'select * from world_table',
cartocss: style
}
],
}, {https:true})
.addTo(map)
.done(function(layers) {
// define sublayer
var sublayer = layers.getSubLayer(0);
// add infowindow interactivity to the sublayer (show cartodb_id and name columns from the table)
cartodb.vis.Vis.addInfowindow(map, sublayer, ['name']);
// add basemap
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png',{
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>',zIndex:100
}).addTo(map);
})
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment