Skip to content

Instantly share code, notes, and snippets.

@michellechandra
Last active May 29, 2019 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michellechandra/154c15793c5d01a43c1e to your computer and use it in GitHub Desktop.
Save michellechandra/154c15793c5d01a43c1e to your computer and use it in GitHub Desktop.
CartoDB.js Hover Infowindow Using CreateLayer

This example map visualization shows how to add a hover infowindow object to a CartoDB map visualization using CartoDB.js.

<!DOCTYPE html>
<html>
<head>
<title>#DrawDisability</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" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
div.cartodb-tooltip {
position: absolute;
width: 350px;
max-width: none;
overflow-y: hidden;
z-index: 50;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function main() {
var map = L.map('map', {
center: [0, 0],
zoom: 2,
zoomControl: true
});
L.tileLayer('http://{s}.tile.stamen.com/watercolor/{z}/{x}/{y}.png', {
attribution: 'Stamen'
}).addTo(map);
cartodb.createLayer(map, 'https://team.cartodb.com/u/chandra/api/v2/viz/0249d564-6d3a-11e5-a49e-0e5db1731f59/viz.json')
.addTo(map)
.on('done', function(layer) {
cartodb.vis.Vis.addInfowindow(map, layer.getSubLayer(0), ['_url', 'name', 'age', 'country', 'city']);
layer.setInteraction(true);
var sublayer = layer.getSubLayer(0);
layer.leafletMap.viz.addOverlay({
type: 'tooltip',
layer: sublayer,
template: '<div class="cartodb-tooltip-content-wrapper"><img style="width: 100%" src={{_url}}>{{name}}, {{age}}, {{city}}, {{country}}</div>',
position: 'bottom|right',
fields: [{ name: 'name' }]
});
}).on('error', function() {
console.log("some error occurred");
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment