Forked from https://gist.github.com/mbostock/9656675
xxxxxxxxxx
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
.feature {
fill: #ccc;
cursor: pointer;
}
.feature.active {
fill: orange;
}
.mesh {
fill: none;
stroke: #fff;
stroke-linecap: round;
stroke-linejoin: round;
}
.calles {
fill: none;
stroke: #a0a0a0;
}
.limites {
fill: none;
stroke: #fff;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="//d3js.org/d3-path.v1.min.js"></script>
<script src="//d3js.org/d3-shape.v1.min.js"></script>
<script>
var width = 960,
height = 500,
active = d3.select(null);
var projection = d3.geo.mercator()
.scale(75000)
.rotate([74.0712581087617,-4.61742011111319])
.translate([width / 2, height / 2]);
var zoom = d3.behavior.zoom()
.translate([0, 0])
.scale(1)
.scaleExtent([1, 25])
.on("zoom", zoomed);
var path = d3.geo.path()
.projection(projection)
.pointRadius(0.7);
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) // delete this line to disable free zooming
.call(zoom.event);
var symbolGen = d3.symbol().size(30).type(d3.symbolCross);
d3.json("ejesviales.topo.json", function(error4, ejesviales) {
if (error4) throw error4;
d3.json("bares.topo.json", function(error3, bares) {
if (error3) throw error3;
d3.json("colegios.topo.json", function(error2, colegios) {
if (error2) throw error2;
d3.json("localidades.topo.json", function(error, us) {
if (error) throw error;
g.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.attr("class", "feature")
.on("click", clicked);
/*g.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);*/
g.append("path")
.datum(topojson.mesh(ejesviales, ejesviales.objects.ejesviales))
.attr("class", "calles")
.attr("d", path)
;
g.append("path")
.datum(topojson.mesh(us, us.objects.states))
.attr("class", "mesh")
.attr("d", path)
;
g.append("path")
.datum(topojson.feature(bares, bares.objects.bares))
.attr("class", "points")
.attr("d", path)
.style("fill","rgb(255,0,0)");
g.append("path")
.datum(topojson.feature(colegios, colegios.objects.colegios))
.attr("class", "points")
.attr("d", path)
//.style("stroke","rgb(0,0,0)")
//.attr("d", symbolGen())
;
});
});
});
});
function clicked(d) {
if (active.node() === this) return reset();
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 = Math.max(1, Math.min(8, 0.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.style("point-radius", 1.5 / d3.event.scale + "px");
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// If the drag behavior prevents the default click,
// also stop propagation so we don’t click-to-zoom.
function stopped() {
if (d3.event.defaultPrevented) d3.event.stopPropagation();
}
</script>
https://d3js.org/d3.v3.min.js
https://d3js.org/topojson.v1.min.js
https://d3js.org/d3-path.v1.min.js
https://d3js.org/d3-shape.v1.min.js