Skip to content

Instantly share code, notes, and snippets.

@ebrelsford
Created March 28, 2017 16:14
Show Gist options
  • Save ebrelsford/72ad72a7a212847f7fbf82c390077ff7 to your computer and use it in GitHub Desktop.
Save ebrelsford/72ad72a7a212847f7fbf82c390077ff7 to your computer and use it in GitHub Desktop.
Hover popups in Carto.js v3, template in HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!--
Load Carto's code so we can pull in our Carto maps.
-->
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
#map {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="template/html" id="tooltip_template">
<div class="cartodb-tooltip-content-wrapper">
<div class="cartodb-tooltip-content">
<h3>Name</h3>
<p>{{name}}</p>
<h3>Watsan</h3>
<p>{{watsan}}</p>
</div>
</div>
</script>
<script>
var dataLayer;
$(document).ready(function () {
//
// Initialize map from CartoDB: create the map and store the data layer for later
//
cartodb.createVis('map', 'https://thenewschool.carto.com/u/brelsfoeagain/api/v2/viz/5d74528a-f60c-45c8-bfde-92ecd4c386d4/viz.json')
.done(function (vis, layers) {
dataLayer = layers[1].getSubLayer(0);
dataLayer.setInteractivity('name, watsan');
vis.addOverlay({
type: 'tooltip',
layer: dataLayer,
template: $('#tooltip_template').html(),
position: 'bottom|right',
fields: [{name: 'name', watsan: 'watsan'}]
});
});
});
</script>
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment