使用 d3.js 缩放和平移以太平洋为中心的墨卡托地图 [英] Zooming and Panning a Mercator Map centered on the Pacific using d3.js
问题描述
如果这是我对显而易见的事情视而不见的简单案例,我深表歉意,但我正在尝试将一个页面放在一起,该页面显示以太平洋为中心的墨卡托投影中的世界地图(数据来自 TopoJSON 文件).IE.左边是欧洲,右边是美国,中间是澳大利亚.有点像这样...
Apologies if this is a simple case of me being blind to the obvious, but I am trying to put together a page that shows a map of the world (data sourced from a TopoJSON file) in Mercator projection centered on the Pacific. I.e. Europe on the left, America on the right and Australia in the middle. A bit like this...
从这一点来看,我希望能够根据自己的意愿缩放和平移地图,但是当我向东或向西平移时,我希望地图四处"滚动而不是来到世界的尽头(我希望这是有道理的).
From this point I want to be able to zoom and pan the map to my hearts desire, but when I pan east or west, I want the map to scroll 'around' and not come to the end of the World (I hope that makes sense).
我目前正在处理的代码在这里(或在以下 Gist (https://gist.github.com/d3noob/4966228)或阻止(http://bl.ocks.org/d3noob/4966228));
The code I am currently working on is here (or at the following Gist (https://gist.github.com/d3noob/4966228) or block (http://bl.ocks.org/d3noob/4966228));
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {font-size:11px;}
path {
stroke: black;
stroke-width: 0.25px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
var width = 960,
velocity = .005,
then = Date.now()
height = 475;
var projection = d3.geo.mercator()
.center([0, 0 ])
.scale(1000);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var path = d3.geo.path()
.projection(projection);
var g = svg.append("g");
d3.json("world-110m.json", function(error, topology) {
g.selectAll("path")
.data(topojson.object(topology, topology.objects.countries).geometries)
.enter()
.append("path")
.attr("d", path)
.style("fill","black")
d3.timer(function() {
var angle = velocity * (Date.now() - then);
projection.rotate([angle,0,0]);
svg.selectAll("path")
.attr("d", path.projection(projection));
});
var zoom = d3.behavior.zoom()
.on("zoom",function() {
g.attr("transform","translate("+d3.event.translate.join(",")+")scale("+d3.event.scale+")")
});
svg.call(zoom)
});
</script>
</body>
</html>
代码是示例的混合体,因此我可以看到可以自动从西向东旋转的地图,并且可以使用鼠标进行平移和缩放,但是在平移和缩放时,据我所知,我正在影响内部g"元素而不是svg"中的地图元素.
The code is an amalgam of examples and as a result I can see a map that can rotate west to east automatically, and I can pan and zoom using the mouse, but when panning and zooming, from what I can tell, I am affecting the internal "g" element and not the map within the "svg" element.
有很多很好的例子可以平移和缩放以子午线为中心的地图.但在我发现的反经络上没有.
There are plenty of good examples of being able to pan and zoom a map centered on the meridian. But none on the anti-meridian that I have discovered.
任何帮助将不胜感激.
推荐答案
我最终解决了同样的问题.这是示例 (见代码),您可以在其中向左/向右平移以旋转投影(带环绕),向上/向下平移(由最大绝对纬度钳制),以及缩放.确保投影始终适合视框.
I ended up working on the same problem. Here's an example (see code) where you pan left/right to rotate the projection (with wraparound), and up/down to translate (clamped by max absolute latitude), with zoom as well. Ensures that projection always fits within viewbox.
我学到了很多关于缩放行为以及投影 center() 和 rotate() 交互的知识.
I learned a lot about zoom behavior, and projection center() and rotate() interaction.
这篇关于使用 d3.js 缩放和平移以太平洋为中心的墨卡托地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!