Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active September 14, 2016 13:01
Show Gist options
  • Save ramiroaznar/567c41b317f06b7bf31341f2ab7c1dc8 to your computer and use it in GitHub Desktop.
Save ramiroaznar/567c41b317f06b7bf31341f2ab7c1dc8 to your computer and use it in GitHub Desktop.
2 x createLayer + layerSource + tooltips with CARTO.js
<!DOCTYPE html>
<html>
<head>
<title>2 x createLayer + layerSource + tooltips with CARTO.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="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<style>
html, body, #map {
width:100%;
height:100%;
padding: 0;
margin: 0;
</style>
</head>
<body>
<div id='map'></div>
<script>
function main() {
// declare a map object
var map = new L.Map('map', {
center: [43, 0],
zoom: 5
});
// add basemap
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: 'CartoDB'
}).addTo(map);
var query1 = "select * from populated_places where adm0name ilike 'france'";
var query2 = "select * from populated_places where adm0name ilike 'spain'";
var style1 = '#layer{marker-fill: blue; marker-width: 5; line-width: 0.2;}'
var style2 = '#layer{marker-fill: red; marker-width: 5; line-width: 0.2;}'
var layerSource1 = {
user_name: 'ramirocartodb',
type: 'cartodb',
sublayers: [{
sql: query1,
https: 'force https',
cartocss: style1,
interactivity: 'cartodb_id, pop_max, name'
}]
}
var layerSource2 = {
user_name: 'ramirocartodb',
type: 'cartodb',
sublayers: [{
sql: query2,
https: 'force https',
cartocss: style2,
interactivity: 'cartodb_id, pop_max, name'
}]
}
// add layer 1st layer
cartodb.createLayer(map, layerSource1, {https:true})
.addTo(map)
// add sublayer
.done(function(layer) {
var cartoLayer1 = layer.getSubLayer(0);
cartoLayer1.setInteraction(true);
var tooltip = layer.leafletMap.viz.addOverlay({
type: 'tooltip',
layer: layer,
template: '<div class="cartodb-tooltip-content-wrapper"> <div class="cartodb-tooltip-content"><h3>{{name}}, France</h3><p>{{pop_max}} inhabitants</p></div></div>',
width: 200,
position: 'bottom|right',
fields: [{ name: 'name' }, { pop_max: 'pop_max' }]
});
$('body').append(tooltip.render().el);
});
// add layer 2nd layer
cartodb.createLayer(map, layerSource2, {https:true})
.addTo(map)
// add sublayer
.done(function(layer) {
var cartoLayer2 = layer.getSubLayer(0);
cartoLayer2.setInteraction(true);
var tooltip = layer.leafletMap.viz.addOverlay({
type: 'tooltip',
layer: layer,
template: '<div class="cartodb-tooltip-content-wrapper"> <div class="cartodb-tooltip-content"><h3>{{name}}, Spain</h3><p>{{pop_max}} inhabitants</p></div></div>',
width: 200,
position: 'bottom|right',
fields: [{ name: 'name' }, { pop_max: 'pop_max' }]
});
$('body').append(tooltip.render().el);
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment