Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Created December 20, 2012 20:51
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 wboykinm/4348449 to your computer and use it in GitHub Desktop.
Save wboykinm/4348449 to your computer and use it in GitHub Desktop.
d3 hover via cartodb sql API and mapbox.js
<!DOCTYPE html>
<html>
<head>
<script src='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'></script>
<link href='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css' rel='stylesheet' />
<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://d3js.org/d3.v2.min.js'></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
.d3-vec { position:absolute; }
path {
fill: #000;
fill-opacity: .2;
stroke: #fff;
stroke-width: 1.5px;
}
path:hover {
fill: brown;
fill-opacity: .7;
}
</style>
<script>
function d3layer() {
var f = {}, bounds, feature, collection;
var div = d3.select(document.body)
.append("div")
.attr('class', 'd3-vec'),
svg = div.append('svg'),
g = svg.append("g");
f.parent = div.node();
f.project = function(x) {
var point = f.map.locationPoint({ lat: x[1], lon: x[0] });
return [point.x, point.y];
};
var first = true;
f.draw = function() {
first && svg.attr("width", f.map.dimensions.x)
.attr("height", f.map.dimensions.y)
.style("margin-left", "0px")
.style("margin-top", "0px") && (first = false);
path = d3.geo.path().projection(f.project);
feature.attr("d", path);
};
f.data = function(x) {
collection = x;
bounds = d3.geo.bounds(collection);
feature = g.selectAll("path")
.data(collection.features)
.enter().append("path");
return f;
};
f.extent = function() {
return new MM.Extent(
new MM.Location(bounds[0][1], bounds[0][0]),
new MM.Location(bounds[1][1], bounds[1][0]));
};
return f;
}
var communes = ('http://dai.cartodb.com/api/v2/sql?q=SELECT%20*%20FROM%20ag_2011_commune3%20&format=geojson&callback=?&foo=geojsonp');
mapbox.auto('map', 'landplanner.map-pd5ea4xu,landplanner.map-lronacbg', function(map) {
l = d3layer().data(communes);
map.addLayer(l);
map.extent(l.extent());
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment