Using the TopoJSON format in connection with D3 to create an interactive worldmap.
Click a country to center and zoom. Click again to zoom out. This example also allows you to freely pan and zoom with the mouse.
forked from cjburgoyne's block: Interactive World Map
xxxxxxxxxx
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
.stroke {
fill: none;
stroke: #000;
stroke-width: 3px;
}
.graticule {
fill: none;
stroke: #777;
stroke-width: .5px;
stroke-opacity: .5;
}
.country {
fill: #ccc;
}
.boundary {
fill: none;
stroke: #fff;
stroke-width: .5px;
}
div.tooltip {
position: absolute;
text-align: center;
color: black;
font-weight: bold;
padding: 2px;
background: white;
border: 2px solid;
border-radius: 8px;
pointer-events: none;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 570,
active = d3.select(null);
var projection = d3.geo.mercator()
.scale(150)
.translate([width / 2, height / 1.5]);
var zoom = d3.behavior.zoom()
.translate([0, 0])
.scale(1)
.scaleExtent([1, 8])
.on("zoom", zoomed);
var path = d3.geo.path()
.projection(projection);
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on("click", stopped, true);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", reset);
var g = svg.append("g");
svg.call(zoom).call(zoom.event);
d3.json("mapfile.json", function (error, world) {
var countries = topojson.feature(world, world.objects.world);
g.selectAll(".country")
.data(countries.features)
.enter().append("path")
.attr("class", function (d) {
return "country " + d.id;
})
.attr("d", path)
.on("click", clicked)
.on("mouseover", function (d) {
d3.select(this).transition().duration(300).style("fill", "#377eb8");
div.transition().duration(300)
.style("opacity", 1)
div.text(d.properties.name)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 30) + "px");
})
.on("mouseout", function (d) {
d3.select(this).transition().duration(300).style("fill", "#ccc");
div.transition().duration(300)
.style("opacity", 0);
});
g.append("path")
.datum(topojson.mesh(world, world.objects.world, function (a, b) {
return a !== b;
}))
.attr("class", "boundary")
.attr("d", path);
});
function clicked(d) {
if (active.node() === this) return reset(); // a second click returns to default zoom
active.classed("active", false);
active = d3.select(this).classed("active", true);
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = .9 / Math.max(dx / width, dy / height),
translate = [width / 2 - scale * x, height / 2 - scale * y];
svg.transition()
.duration(750)
.call(zoom.translate(translate).scale(scale).event);
}
function reset() {
active.classed("active", false);
active = d3.select(null);
svg.transition()
.duration(750)
.call(zoom.translate([0, 0]).scale(1).event);
}
function zoomed() {
g.style("stroke-width", 1.5 / d3.event.scale + "px");
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
function stopped() {
if (d3.event.defaultPrevented) d3.event.stopPropagation();
}
</script>
</body>
Modified http://d3js.org/d3.v3.min.js to a secure url
Modified http://d3js.org/d3.geo.projection.v0.min.js to a secure url
Modified http://d3js.org/topojson.v1.min.js to a secure url
https://d3js.org/d3.v3.min.js
https://d3js.org/d3.geo.projection.v0.min.js
https://d3js.org/topojson.v1.min.js