gpt4 book ai didi

d3.js - 如何将纬度、经度数据动态放置到 topoJSON map 上

转载 作者:行者123 更新时间:2023-12-01 06:31:28 25 4
gpt4 key购买 nike

我有 WV 县的 topoJSON map 。它正确地显示了人口密度。该 map 是根据 geoJSON 数据创建的,然后使用 topojson 进行转换。这是开发人员为创建其数据的静态显示而采取的所有正常步骤。我现在的问题是,这张 map 的用户想要在上面放置数据,他们只有经纬度值。如何在之前创建的 topoJSON map 上显示经纬度数据?

以下是一些示例数据:

var sampleData = [
{"name": "Weirton", "coordinates": [-80.589517, 40.418957]},
{"name": "Morgantown", "coordinates": [-79.955897, 39.629526]},
{"name": "Shepherdstown", "coordinates": [-77.804161, 39.430100]}
];

我试图在我的(已经显示的) map 上显示,如下所示:
var group = svg.append('g');

group.selectAll("cities")
.data(sampleData)
.enter()
.append("cities")
.attr("class", "cities")
.attr("transform", function(d) {return "translate(" + projection(d.coordinates) + ")";});

是否可以将一些经纬度数据放到静态 map 上,或者我是否犯了一些编程错误?

更新:我玩过数据并发现使用此代码:
svg.append("path")
.datum(topojson.feature(wv, dynamicData))
.attr("d", path)
.attr("class", "place")

使用这些数据
var dynamicData = 
{
"type":"GeometryCollection",
"geometries":[
{"type":"Point","properties":{"NAME":"Weirton"},"id":"Weirton","coordinates":[4172,9359]},
{"type":"Point","properties":{"NAME":"Morgantown"},"id":"Morgantown","coordinates":[5458,7063]},
{"type":"Point","properties":{"NAME":"Shepherdstown"},"id":"Shepherdstown","coordinates":[9826,6483]}]
};

但不适用于此数据
var dynamicData = 
{
"type":"GeometryCollection",
"geometries":[
{"type":"Point","properties":{"NAME":"Weirton"},"id":"Weirton","coordinates":[-80.589517, 40.418957]},
{"type":"Point","properties":{"NAME":"Morgantown"},"id":"Morgantown","coordinates":[-79.955897, 39.629526]},
{"type":"Point","properties":{"NAME":"Shepherdstown"},"id":"Shepherdstown","coordinates":[-77.804161, 39.430100]}]
};

有人可以告诉我如何计算新坐标吗?

最佳答案

好的,事实证明这段代码将正确显示我的纬度/经度数据

svg.selectAll(".geojson").data([sampleData4])
.enter()
.append("path")
.attr("class","geojson")
.attr("d", path)
;

(sampleData4 是我编辑中的以上经纬度数据(即动态数据)。经过多次谷歌搜索后,我偶然发现了这一点。我在
http://hashbang.co.nz/blog/2013/2/25/d3_js_geo_fun

我很感激任何人解释它为什么起作用以及为什么我以前的所有工作都没有。我也仍然对了解(通过一个详细的具体示例)坐标转换的实际工作方式非常感兴趣。

关于d3.js - 如何将纬度、经度数据动态放置到 topoJSON map 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244656/

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