gpt4 book ai didi

javascript - 使用 d3.geo.path() 在 D3 中映射弧

转载 作者:行者123 更新时间:2023-11-29 14:45:38 25 4
gpt4 key购买 nike

我正在尝试绘制一条连接美国 map 上两点的弧线。我用来制作美国 map 的代码是

var path = d3.geo.path()
.projection(projection);

var graticule = d3.geo.graticule()
.extent([[-98 - 45, 38 - 45], [-98 + 45, 38 + 45]])
.step([5, 5]);

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);

queue()
.defer(d3.json,'us.json')
.await(makeMyMap);



function makeMyMap(error, us) {
if (error) throw error;

svg.insert("path", ".graticule")
.datum(topojson.feature(us, us.objects.land))
.attr("class", "land")
.attr("d", path);

svg.insert("path", ".graticule")
.datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b && !(a.id / 1000 ^ b.id / 1000); }))
.attr("class", "county-boundary")
.attr("d", path);

svg.insert("path", ".graticule")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "state-boundary")
.attr("d", path);

drawPath()

}

function drawPath() {
var route = svg.insert("path", ".graticule")
.datum({type: "LineString", coordinates: [[33,-118], [38.6,-78]]})
.attr("class", "route")
.attr("d", path);
}

目前正在某处绘制由drawPath() 函数绘制的路径,但我无法在 map 上查看它。如果我不在 CSS 中设置 fill: none,那么屏幕将变黑,但将其设置为一种颜色只会使 Canvas 被该颜色覆盖。

us.json文件是用来制作 map 的,是一个topojson对象。

最佳答案

您搞砸了 LineString 的位置。根据spec位置指定为 [longitude,latitude]。由于纬度值不能超过 90 度,很明显您需要切换坐标值的顺序:

.datum({type: "LineString", coordinates: [[-118,33], [-78,38.6]]})

感谢 Mark 的评论,他花时间并付出了努力,这也可以在他的 working demo 中找到。 .

关于javascript - 使用 d3.geo.path() 在 D3 中映射弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33343993/

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