Skip to content

Instantly share code, notes, and snippets.

@phil-pedruco
Created September 11, 2013 11:25
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 phil-pedruco/6522279 to your computer and use it in GitHub Desktop.
Save phil-pedruco/6522279 to your computer and use it in GitHub Desktop.
Zoomable world map with highlighted locations
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
#country {
fill: #aaa;
}
#country-borders {
fill: none;
stroke: #fff;
stroke-width: 1.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.scale(100)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var zoom = d3.behavior.zoom()
.translate(projection.translate())
.scale(projection.scale())
.scaleExtent([100, 1000])
.on("zoom", zoomed);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var g = svg.append("g")
.call(zoom);
var dotSpot = [[20,20], [10,10]];
console.log(projection(dotSpot[0])[0])
g.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height);
d3.json("world.json", function(error, nations) {
console.log(nations)
g.append("g")
.attr("id", "country")
.selectAll("path")
.data(topojson.feature(nations, nations.objects.countries).features)
.enter().append("path")
.attr("d", path);
g.append("path")
.datum(topojson.mesh(nations, nations.objects.countries, function(a, b) { return a !== b; }))
.attr("id", "country-borders")
.attr("d", path);
g.selectAll("circle")
.data(dotSpot).enter()
.append("circle")
.attr("cx", function (d,i) { return projection(d)[0]; })
.attr("cy", function (d,i) { return projection(d)[1]; })
.attr("r", "10px")
.style("fill", "red");
});
function zoomed() {
projection.translate(d3.event.translate).scale(d3.event.scale);
g.selectAll("path").attr("d", path)
g.selectAll("circle")
.attr("cx", function (d,i) { return projection(d)[0]; })
.attr("cy", function (d,i) { return projection(d)[1]; })
.attr("r", "10px")
.style("fill", "red");
}
</script>
Display the source blob
Display the rendered blob
Raw
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