gpt4 book ai didi

d3.js - 如何使用 d3 在传单中显示 topojson 层?

转载 作者:行者123 更新时间:2023-12-04 07:54:58 24 4
gpt4 key购买 nike

在过去的两天里,我一直在尝试在 Leaflet 上制作一个非常简单的 map ,但正在撞墙。

我有一个由以前的 geoJSON 文件组成的两层 topoJSON 文件:美国 5 个州的邮政编码,以及 5 个州的多边形。

我想在 Leaflet 上显示这些,由于邮政编码层的文件大小较小,因此使用 topoJSON 而不是 geoJSON 很重要。

问题是我一生都无法在我的 topoJSON 文件中将较小的州图层显示在 map 上。我在网上看了很多例子,并遵循了 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 的边界;

var bounds = d3.geo.bounds(topojson.feature(states_and_codes, states_and_codes.objects.claim_states));

您还会发现 here大量来自源头的优秀 TopoJson 块!

关于d3.js - 如何使用 d3 在传单中显示 topojson 层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17662530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com