Last active
December 29, 2015 22:19
-
-
Save jsanz/a89ba37800b9b280146c to your computer and use it in GitHub Desktop.
CartoDB.js named map with custom infowindow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Named Maps Tutorial | CartoDB</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" /> | |
<style> | |
html, body, #map { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
</style> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" /> | |
</head> | |
<body> | |
<div id="map"></div> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script> | |
<!-- Infowindow template --> | |
<script type="infowindow/html" id="infowindow_template"> | |
<div class="cartodb-popup header blue v2"> | |
<a href="#close" class="cartodb-popup-close-button close">x</a> | |
<div class="cartodb-popup-content-wrapper"> | |
Custom infowindow | |
<ul> | |
<li><%= cartodb_id %></li> | |
<li><%= name %></li> | |
<li><%= pop_max.toFixed(2) %></li> | |
<li><%= customFunction(pop_max) %></li> | |
<%= '<script>alert(' + cartodb_id + ')</scr' + 'ipt>' %> | |
</ul> | |
</div> | |
<div class="cartodb-popup-tip-container"> | |
</div> | |
</div> | |
</script> | |
<!-- Drop your code between the script tags below! --> | |
<script> | |
/* Global function to be used on infowindow template */ | |
window.customFunction = function(number){ | |
return number/2; | |
} | |
function main() { | |
// create leaflet map | |
var map = L.map('map', { | |
zoomControl: false, | |
scrollWheelZoom: false, | |
center: [0, 0], | |
zoomControl: true, | |
zoom: 3 | |
}); | |
// add a base layer | |
L.tileLayer('http://tile.stamen.com/toner/{z}/{x}/{y}.png', { | |
attribution: 'Stamen' | |
}).addTo(map); | |
// add cartodb namedmap layer with one sublayer | |
cartodb.createLayer(map, { | |
user_name: 'documentation', | |
type: 'namedmap', | |
named_map: { | |
name: "namedmap_tutorial", | |
layers: [{ | |
layer_name: "t", | |
interactivity: "cartodb_id, name, pop_max" | |
}] | |
} | |
}) | |
.addTo(map) | |
.done(function(layer) { | |
layer.getSubLayer(0).setInteraction(true); | |
// show infowindows on click | |
cdb.vis.Vis.addInfowindow( | |
map, | |
layer.getSubLayer(0), | |
['cartodb_id','name', 'pop_max'],{ | |
'sanitizeTemplate':false | |
}).model.set({ | |
'template' : function(object){ | |
if (object && object.cartodb_id){ | |
return _.template($('#infowindow_template').html())(object); | |
} | |
} | |
}); | |
}); | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment