Skip to content

Instantly share code, notes, and snippets.

@rochoa
Forked from rochoa/index.html
Last active March 16, 2017 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rochoa/c24a22c82b3a9c373a9a to your computer and use it in GitHub Desktop.
Save rochoa/c24a22c82b3a9c373a9a to your computer and use it in GitHub Desktop.
[CARTO] CartoDB.js + Leaflet interactivity example
<!DOCTYPE html>
<html>
<head>
<title>CartoDB + Leaflet interactivity example | 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="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/3.11/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.11/cartodb.uncompressed.js"></script>
<script>
function main() {
// create leaflet map
var map = L.map('map', {
zoomControl: false,
center: [43, 0],
zoom: 3
})
// add a base layer
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png',{
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
// add cartodb layer with one sublayer
cartodb.createLayer(map, {
user_name: 'rochoa',
type: 'namedmap',
options: {
named_map: {
/* this is the name you get when you create the template:
curl -X POST \
-H "Content-Type: application/json" \
-d @template.json \
"https://rochoa.cartodb.com/api/v1/map/named?api_key=${CDB_API_KEY}"
#response:
{"template_id":"rochoa@example_world_borders_feature_click"}
*/
name: 'rochoa@example_world_borders_feature_click',
// params have to be defined in the placeholders, see template_params.json
// and the be inside the style or the sql query, in this case: <%= color %>
params: {
"color": "#5CA2D1"
},
// this is a workaround due to a known bug
layers: [{}]
}
}
})
.addTo(map)
.done(function(layer) {
layer.setInteraction(true);
layer.on('featureClick', function(e, latlng, pos, data, layer) {
console.log("click", latlng, data);
});
});
}
// you could use $(window).load(main);
window.onload = main;
</script>
</body>
</html>
{
"version": "0.0.1",
"name": "example_world_borders_feature_click",
"auth": {
"method": "open"
},
"placeholders": {
"color": {
"type": "css_color",
"default": "#FF6600"
}
},
"layergroup": {
"version": "1.2.0",
"layers": [
{
"type": "cartodb",
"options": {
"cartocss_version": "2.3.0",
"cartocss": "#layer { polygon-fill: <%= color %>; polygon-opacity: 1; line-color: #FFF; line-width: 1; line-opacity: 1; }",
"sql": "select * from world_borders",
"interactivity": ["cartodb_id", "name"]
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment