Skip to content

Instantly share code, notes, and snippets.

Created June 25, 2012 22:23
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
D3 + ModestMaps
<!DOCTYPE html>
<style type="text/css">
#map {
width: 1000px;
height: 600px;
path {
fill: #000;
fill-opacity: .2;
stroke: #fff;
stroke-width: 1.5px;
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<div id="map"></div>
<script type="text/javascript">
var MM=com.modestmaps;
var template = "http://{S}{Z}/{X}/{Y}.png";
var provider = new MM.TemplatedLayer(template, ['a', 'b', 'c', 'd']);
var map = new MM.Map("map", provider);
map.setCenterZoom(new MM.Location(37.8, -96.9), 4);
var svg ="#map").append("svg"),
g = svg.append("g");"position", "absolute");
var project = function(x) {
var point = map.locationPoint(new MM.Location(x[1], x[0]));
return [point.x, point.y];
d3.json("us-states.json", function(collection) {
var bounds = d3.geo.bounds(collection),
path = d3.geo.path().projection(project);
var feature = g.selectAll("path")
var reset = function(m) {
var bottomLeft = project(bounds[0]),
topRight = project(bounds[1]);
svg .attr("width", topRight[0] - bottomLeft[0])
.attr("height", bottomLeft[1] - topRight[1])
.style("margin-left", bottomLeft[0] + "px")
.style("margin-top", topRight[1] + "px");
g .attr("transform", "translate(" + -bottomLeft[0] + "," + -topRight[1] + ")");
feature.attr("d", path);
map.addCallback("drawn", reset);
Display the source blob
Display the rendered blob
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment