gpt4 book ai didi

d3.js - 无法使 d3 map 正确居中

转载 作者:行者123 更新时间:2023-12-04 00:07:28 29 4
gpt4 key购买 nike

我有一张在几个位置之间来回缩放的 map 。但是,我的 map 坐标已关闭。默认情况下, map 似乎以 [0,0] 为中心。我将 projection.center 更改为一个新点,但这引起了一些困惑——( map 开始缩放到其他大陆......)

Here is a plunker以 map 为中心,以 0,0 为中心(鼠标悬停,您将在底部看到坐标)。

我也尝试在 zoomTo 函数中设置 projection.center ,但这又导致了另一个问题。我觉得这应该是一个快速的编辑,但我花了很多时间来解决这个问题。

有什么需要改变的想法吗?

非常感谢。

代码如下:

    <!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
margin: 0;
}

#container {
position: relative;
overflow: hidden;
}

#map{
width:100%;
height:100%;
}

.layer {
position: absolute;
}

.tile {
pointer-events: none;
position: absolute;
width: 256px;
height: 256px;
}

.info {
position: absolute;
bottom: 0px;
left: 0px;
padding: 20px;
background: #000;
color: #fff;
width: 100%;
z-index: 1000;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.tile.v0.min.js"></script>
<div id="canvas">
<div id="container">
<div id="map">
<div class="layer"></div>
</div>
</div>
</div>
<script>

var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight),
prefix = prefixMatch(["webkit", "ms", "Moz", "O"]);

var tile = d3.geo.tile()
.size([width, height]);

var sf = [-122.417, 37.775],
belowsf = [-122.510962, 37.580284];

var projection = d3.geo.mercator()
.scale((1 << 18) / 2 / Math.PI)
.translate([-width / 2, -height / 2]); // just temporary

var map = d3.select("#map");

var layer = d3.select(".layer");

var zoom = d3.behavior.zoom().on("zoom", zoomed);

var canvas = d3.select("#canvas")
.style("width", width + "px")
.style("height", height + "px");

var container = d3.select("#container")
.style("width", width + "px")
.style("height", height + "px")
.on("mousemove", mousemoved);

canvas
.call(zoomTo(sf).event)
.transition()
.duration(10000)
.each(jump);

var info = map.append("div")
.attr("class", "info");

function zoomTo(place) {
return zoom
.scale(projection.scale() * 2 * Math.PI)
.translate(projection(place).map(function(x) { return -x; }));


}

function jump() {
var t = d3.select(this);
(function repeat() {
t = t.transition()
.call(zoomTo(belowsf).event)
.transition()
.call(zoomTo(sf).event)
.each("end", repeat);
})();
}


function mousemoved() {
info.text(formatLocation(projection.invert(d3.mouse(this)), zoom.scale()));
}

function zoomed() {
var tiles = tile
.scale(zoom.scale())
.translate(zoom.translate())
();

var image = layer
.style(prefix + "transform", matrix3d(tiles.scale, tiles.translate))
.selectAll(".tile")
.data(tiles, function(d) { return d; });

image.exit()
.remove();

image.enter().append("img")
.attr("class", "tile")
.attr("src", function(d) { return "http://" + ["a", "b", "c"][Math.random() * 3 | 0] + ".basemaps.cartocdn.com/light_all/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.style("left", function(d) { return (d[0] << 8) + "px"; })
.style("top", function(d) { return (d[1] << 8) + "px"; });
}


function matrix3d(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "matrix3d(" + [k, 0, 0, 0, 0, k, 0, 0, 0, 0, k, 0, r(translate[0] * scale), r(translate[1] * scale), 0, 1 ] + ")";
}

function prefixMatch(p) {
var i = -1, n = p.length, s = document.body.style;
while (++i < n) if (p[i] + "Transform" in s) return "-" + p[i].toLowerCase() + "-";
return "";
}

function formatLocation(p, k) {
var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f");
return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " "
+ (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E");
}

</script>
</body>
</html>

屏幕截图,显示不正确的坐标: enter image description here

最佳答案

好的,我想我明白了。对于您关于 map 居中的第一个问题,您可以使用旧金山坐标的投影来转换缩放:

var projection = d3.geo.mercator()
.scale((1 << 18) / 2 / Math.PI)
.translate([width / 2, height / 2]);

var center = projection(sf);

var zoom = d3.behavior.zoom()
.scale(projection.scale() * 2 * Math.PI)
.translate([width - center[0], height - center[1]]);

设置中心后,您需要更新投影以与缩放对齐,以便在屏幕上显示正确的坐标:

projection
.scale(zoom.scale() / 2 / Math.PI)
.translate(zoom.translate());

这是我在加载 map 时得到的结果,在我看来,这就是您正在寻找的坐标:

enter image description here

我还修改了 ZoomTo 函数以在过渡时考虑上述变化:

function zoomTo(place) {
// project back to main projection
projection = d3.geo.mercator()
.scale((1 << 18) / 2 / Math.PI)
.translate([width / 2, height / 2])
// set new center
center = projection(place);
// set zoom with new center
return zoom
.scale(projection.scale() * 2 * Math.PI)
.translate([width - center[0], height - center[1]]);



}

完整代码:http://plnkr.co/edit/eE0XZQPHB6rAc9cg4LMH?p=preview

关于d3.js - 无法使 d3 map 正确居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32066660/

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