Skip to content

Instantly share code, notes, and snippets.

@cspanring
Last active December 26, 2015 19:39
Show Gist options
  • Save cspanring/7202839 to your computer and use it in GitHub Desktop.
Save cspanring/7202839 to your computer and use it in GitHub Desktop.
Basic CartoDB, Leaflet, UTFGrid example.
<!DOCTYPE html>
<html>
<head>
<title>Leaflet CartoDB Example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id="map"></div>
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
<script src="http://danzel.github.io/Leaflet.utfgrid/src/leaflet.utfgrid.js"></script>
<script>
var map = L.map('map').setView([42.373611, -71.110556], 12);
L.tileLayer('http://{s}.tile.stamen.com/toner-background/{z}/{x}/{y}.png', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
subdomains: 'abcd',
minZoom: 0,
maxZoom: 20
}).addTo(map);
L.tileLayer('http://cspanring.cartodb.com/tiles/cambridge_neighborhoods/{z}/{x}/{y}.png', {
attribution: 'City of Cambridge, MA',
minZoom: 9,
maxZoom: 12
}).addTo(map);
var utfGrid = new L.UtfGrid('http://cspanring.cartodb.com/tiles/cambridge_neighborhoods/{z}/{x}/{y}.grid.json?callback={cb}&interactivity=name');
map.addLayer(utfGrid);
utfGrid.on('click', function (e) {
if (e.data) {
alert( 'Cambridge Neighorhood: ' + e.data.name );
} else {
alert( 'Not a Cambridge Neighorhood.' );
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment