Last active
December 29, 2015 23:05
-
-
Save jsanz/d6831b16303567c2af07 to your computer and use it in GitHub Desktop.
CartoDB.js named map with custom infowindo (mustache)
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 google maps library --> | |
<script type="text/javascript" src="http://www.maps.google.com/maps/api/js?sensor=false&v=3.12"></script> | |
<!-- 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> | |
<!-- Infowindow template --> | |
<script type="infowindow/html" id="infowindow_template2"> | |
<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 }}</li> | |
{{{ script_call }}} | |
</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() { | |
var myLatlng = new google.maps.LatLng(0,0); | |
var myOptions = { | |
zoom: 3, | |
center: myLatlng, | |
disableDefaultUI: true, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
} | |
// Render basemap | |
map = new google.maps.Map(document.getElementById("map"), myOptions); | |
// 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'],{ | |
}).model.set({ | |
'sanitizeTemplate':false, | |
'template' : function(object){ | |
if (object && object.cartodb_id){ | |
object['script_call'] = '<script>alert("' + object.cartodb_id + '")</scri' + 'pt>'; | |
return Mustache.render($('#infowindow_template2').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