如何使用d3在leaflet中显示一个topojson图层?

8
我已经试图在过去两天内使用Leaflet制作一个非常简单的地图,但一直卡壳。
我有一个topoJSON文件,其中包含两个图层,分别由以前的geoJSON文件制成:5个州的美国邮政编码和这5个州的多边形。
我希望在Leaflet上显示它们,并且使用topoJSON而不是geoJSON非常重要,因为它具有比邮政编码图层更小的文件大小。
问题是我无法让即使是文件中较小的州层在地图上显示。我查看了网络上的很多例子并关注了Mike Bostock的示例:https://github.com/mbostock/bost.ocks.org/blob/gh-pages/mike/leaflet/index.html#L131-171
我可以使用d3在web浏览器中显示文件,所以文件没有问题。我正在使用topoJSON的v1版本以及脚本中的topojson.feature方法。代码如下。我无法提供topoJSON文件,但我认为它没问题,因为我以前用过它来制作d3。如果有人能在脚本中发现什么问题,那就太好了。
谢谢。
        <!DOCTYPE html>
<meta charset="utf-8">
<head> 
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://d3js.org/d3.v2.min.js?2.9.3"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<title>Gulf Zip Codes</title> 
</head>

<div id="map"></div>
<style type="text/css">
#map {
  height: 800px;
}

path {
  fill: #000;
  fill-opacity: .1;
  stroke: #fff;
  stroke-width: 1.5px;
}

path:hover {
  fill: #1f77b4;
  fill-opacity: .4;
}


</style>
<body>

<script>

var map = L.map('map').setView([32.546813, -88.374023], 6);

L.tileLayer('http://{s}.tile.cloudmade.com/1a1b06b230af4efdbb989ea99e9841af/998/256/{z}/{x}/{y}.png', {

attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

var svg = d3.select(map.getPanes().overlayPane).append("svg"),
    g = svg.append("g").attr("class", "leaflet-zoom-hide");

d3.json("states_and_codes.json", function(error, states_and_codes) {

  var bounds = d3.geo.bounds(states_and_codes);
    path = d3.geo.path().projection(project);

  var feature = g.selectAll("path")
      .data(topojson.feature(states_and_codes,      states_and_codes.objects.claim_states).features)
    .enter().append("path")
    .attr("class", "path")
    .attr("d",path);

  map.on("viewreset", reset);
  reset();

  // Reposition the SVG to cover the features.
  function reset() {
    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);
  }

  // Use Leaflet to implement a D3 geographic projection.
  function project(x) {
    var point = map.latLngToLayerPoint(new L.LatLng(x[1], x[0]));
    return [point.x, point.y];
  } 

});



</script>
</body>

你检查过 topojson.feature() 调用的输出了吗?它看起来正确吗?你确定键值 claim_states 是正确且可用的吗? - nrabinowitz
1个回答

7
如果你还在寻找答案,或者对于其他人来说,这应该是缺失的部分 - TopoJson 的边界。
var bounds = d3.geo.bounds(topojson.feature(states_and_codes, states_and_codes.objects.claim_states));

您还可以在此处找到许多与TopoJson源相关的优秀块,请点击这里


我在Github上放了一个带有示例数据的完整示例。如果JSON无法加载,请尝试查看原始版本(<>图标)。 - Édouard Lopez
谢谢您!我已经寻找正确的方法一个小时了! - Sleenee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接