这让我疯了,我不知道我的错误在哪里,在json文件中(看起来不像),加载地图的代码,甚至是应用的颜色。 我已经与noob d3.js world example进行比较,这对我有用,我只是替换坐标,json地图文件和我要显示的区域的标签,而且瞧不起,呵呵。
这是我的html + js:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: green;
stroke-width: 0.25px;
fill: grey;
}
</style>
<body>
<!--<script src="http://d3js.org/d3.v3.min.js"></script>-->
<!--<script src="http://d3js.org/topojson.v0.min.js"></script>-->
<script type="text/javascript" src="js/d3.v3.js"></script>
<script src="js/topojson.v0.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.center([-3.5,40.5])
.scale(40000)
// .scale(900)
// .rotate([-180,0]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var path = d3.geo.path()
.projection(projection);
var g = svg.append("g");
//d3.json("maps/world-110m2.json", function(error, topology) {
//d3.json("maps/prov_37.json", function(error, topology) {
//d3.json("maps/prov_40.json", function(error, topology) {
d3.json("maps/prov_28.json", function(error, topology) {
g.selectAll("path")
// .data(topojson.object(topology, topology.objects.countries)
// .data(topojson.object(topology, topology.objects.prov_40)
// .data(topojson.object(topology, topology.objects.prov_37)
.data(topojson.object(topology, topology.objects.prov_28)
.geometries)
.enter()
.append("path")
.attr("d", path)
});
var zoom = d3.behavior.zoom()
.on("zoom",function() {
g.attr("transform","translate("+
d3.event.translate.join(",")+")scale("+d3.event.scale+")");
g.selectAll("path")
.attr("d", path.projection(projection));
});
svg.call(zoom)
</script>
</body>
</html>
好吧,我在代码中尝试过的json文件可以从这里下载(以及html文件):test files
请注意,我的每个json地图都有不同的ID或标签用于显示的区域,因此每次我在HTML代码中切换json地图时,我都会切换到正确的ID /标记。例如,地图“prov_28_topo.json”的ID是“prov_28”:
},
"objects": {
"prov_28": {
"type": "GeometryCollection",
"geometries": [{
答案 0 :(得分:1)
我只看了你的一个json文件prov_28_topo.json
,它是无效的JSON。
你有{"type":"Topology","transform":{"scale":[0.0007357596319631963,0.0004301291039103907],"translate":[-4.57907617,38.02244023]},"objects":{"prov_28:{"type"
应该是:
{"type":"Topology","transform":{"scale":[0.0007357596319631963,0.0004301291039103907],"translate":[-4.57907617,38.02244023]},"objects":{"prov_28":{"type"
_28
之后的引用。改变使一切运转起来。