如果我平移地图,标记将显示在地图上,但是如果我缩小然后放大没有标记的地图副本,它们将不会显示,直到我再次平移。
是否可以更改此设置,以便放大和缩小将导致标记在地图上重新计算?
L.map('map', {
'center': [0, 0],
'zoom': 0,
'worldCopyJump': true,
'layers': [
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
'attribution': 'Map data © OpenStreetMap contributors'
}),
L.marker([0, -135]),
L.marker([0, -90]),
L.marker([0, -45]),
L.marker([0, 0]),
L.marker([0, 45]),
L.marker([0, 90]),
L.marker([0, 135])
]
});
body {
margin: 0;
}
html, body, #map {
height: 100%
}
<link href="https://unpkg.com/leaflet/dist/leaflet.css" rel="stylesheet"/>
<script src="https://unpkg.com/leaflet/dist/leaflet-src.js"></script>
<div id="map"></div>
http://embed.plnkr.co/mWKc4M/
如果我平移地图,标记将出现在地图上
具体而言,此行为仅在拖动地图时发生(而不是在通过任何其他方法平移地图时,例如使用键盘快捷键(时不会发生。这是因为,在内部,worldCopyJump
功能是在src/map/handler/Map.Drag.js
处的拖动处理程序中定义的:
// TODO refactor, move to CRS
// @option worldCopyJump: Boolean = false
// With this option enabled, the map tracks when you pan to another "copy"
// of the world and seamlessly jumps to the original one so that all overlays
// like markers and vector layers are still visible.
worldCopyJump: false,
(请注意,传单解释了什么是地图处理程序以及它们是如何工作的(
按照现在的代码,worldCopyJump
功能仅影响拖动处理程序,并通过在每次更新地图拖动处理程序时重置拖动偏移量(而不是地图中心(来工作:
if (map.options.worldCopyJump) {
this._draggable.on('predrag', this._onPreDragWrap, this);
map.on('zoomend', this._onZoomEnd, this);
map.whenReady(this._onZoomEnd, this);
}
/* snip */
_onPreDragWrap: function () {
// TODO refactor to be able to adjust map pane position after zoom
var worldWidth = this._worldWidth,
halfWidth = Math.round(worldWidth / 2),
dx = this._initialWorldOffset,
x = this._draggable._newPos.x,
newX1 = (x - halfWidth + dx) % worldWidth + halfWidth - dx,
newX2 = (x + halfWidth + dx) % worldWidth - halfWidth - dx,
newX = Math.abs(newX1 + dx) < Math.abs(newX2 + dx) ? newX1 : newX2;
this._draggable._absPos = this._draggable._newPos.clone();
this._draggable._newPos.x = newX;
},
那么,该怎么办呢?一种选择是利用wrapLatLng
在每个zoomend
事件上重置地图中心,例如:
map.on('zoomend', function(ev){
map.panTo(map.wrapLatLng(map.getCenter()), {animate: false})
});
这应该可以工作。查看工作演示。
作为替代方法,请考虑使用 https://gitlab.com/IvanSanchez/Leaflet.RepeatedMarkers ,它将每 360° 经度创建每个标记的副本。