Skip to content

Instantly share code, notes, and snippets.

@ernesmb
Last active February 6, 2016 12:22
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 ernesmb/93c9c083f3b0897c5c75 to your computer and use it in GitHub Desktop.
Save ernesmb/93c9c083f3b0897c5c75 to your computer and use it in GitHub Desktop.
Two sublayers / Two infowindows
<!DOCTYPE html>
<html>
<head>
<title>Custom infowindow 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" />
<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, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="infowindow/html" id="infowindow_template">
<div class="cartodb-popup">
<a href="#close" class="cartodb-popup-close-button close">x</a>
<div class="cartodb-popup-content-wrapper">
<div class="cartodb-popup-content">
<h2>This is sublayer 0</h2>
<h4>This is...</h4>
<p>{{content.data.name}}</p>
<h4>It has a population of</h4>
<p>{{content.data.pop_max}}</p>
</div>
</div>
<div class="cartodb-popup-tip-container"></div>
</div>
</script>
<script type="infowindow/html" id="infowindow_template_1">
<div class="cartodb-popup">
<a href="#close" class="cartodb-popup-close-button close">x</a>
<div class="cartodb-popup-content-wrapper">
<div class="cartodb-popup-content">
<h2>This is sublayer 1</h2>
<h4>This is...</h4>
<p>{{content.data.name}}</p>
<h4>It has a population of</h4>
<p>{{content.data.pop_max}}</p>
</div>
</div>
<div class="cartodb-popup-tip-container"></div>
</div>
</script>
<script>
function main() {
var map = L.map('map', {
zoomControl: false,
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 WHERE megacity=0',
cartocss: '/** bubble visualization */ #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: #99CC00; marker-allow-overlap: true; marker-clip: false; marker-comp-op: multiply;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 35676000] { marker-width: 18.0;} #ne_10m_populated_places_simple_7_copy [ pop_max = 2769072] { marker-width: 16.4;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 1262000] { marker-width: 14.9;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 578470] { marker-width: 13.3;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 45604] { marker-width: 11.8;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 139843] { marker-width: 10.2;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 58877] { marker-width: 8.7;} #ne_10m_populated_places_simple_7 [ pop_max <= 2097] { marker-width: 7.1;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 41316] { marker-width: 5.6;} #ne_10m_populated_places_simple_7_copy [ pop_max <= 1741] { marker-width: 4.0;}',
interactivity: 'cartodb_id, name, pop_max'
},
{
sql: 'SELECT * FROM ne_10m_populated_places_simple_7_copy WHERE megacity=1',
cartocss: '#ne_10m_populated_places_simple_7_copy{ marker-fill-opacity: 0.5; marker-line-color: #F00; marker-line-width: 1; marker-line-opacity: 1; marker-placement: point; marker-multi-policy: largest; marker-type: ellipse; marker-fill: #666; marker-allow-overlap: true; marker-clip: false; marker-comp-op: multiply; marker-width:16;}',
interactivity: 'cartodb_id, name, pop_max'
}]
},{https:true})
.addTo(map)
.on('done', function(layer) {
var sublayer0 = layer.getSubLayer(0);
cdb.vis.Vis.addInfowindow(map, sublayer0,
['cartodb_id', 'name', 'pop_max'],
{infowindowTemplate: $('#infowindow_template').html()});
var sublayer1 = layer.getSubLayer(1);
cdb.vis.Vis.addInfowindow(map, sublayer1,
['cartodb_id', 'name', 'pop_max'],
{infowindowTemplate: $('#infowindow_template_1').html()});
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment