-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathcreateLayer_custom_tooltip.html
70 lines (57 loc) · 2.02 KB
/
createLayer_custom_tooltip.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html>
<head>
<title>Tooltip on hover with createLayer() | 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="stylesheet" href="https://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<script src="https://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>
<script type="tooltip/html" id="tooltip_template">
<div class="cartodb-tooltip-content-wrapper">
<div class="cartodb-tooltip-content">
<h3>Name</h3>
<p>{{name}}</p>
<h3>Population</h3>
<p>{{pop2005}}</p>
</div>
</div>
</script>
<div id='map'></div>
<script type="text/javascript">
function main() {
var map = new L.Map('map', {center: [20, 20], zoom: 2});
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="https://carto.com/attribution/">CartoDB</a>'
}).addTo(map);
cartodb.createLayer(map, 'http://documentation.cartodb.com/api/v2/viz/2b13c956-e7c1-11e2-806b-5404a6a683d5/viz.json')
.addTo(map)
.on('done', function(layer) {
//do stuff
var sublayer = layer.getSubLayer(0);
sublayer.setInteractivity('cartodb_id, name, pop2005');
// tooltip definition for createLayer()
var testTooltip = layer.leafletMap.viz.addOverlay({
type: 'tooltip',
layer: sublayer,
template: $('#tooltip_template').html(),
width: 200,
position: 'bottom|right',
fields: [{ name: 'name', population: 'pop2005' }]
});
$('body').append(testTooltip.render().el);
});
}
window.onload = main;
</script>
</body>
</html>