【发布时间】:2014-04-27 14:57:00
【问题描述】:
我希望我的标记在弹出窗口的中心居中.. 并且不在标记 latlng 中居中地图,而是在标记和弹出窗口的中心! 问题是弹出窗口具有动态内容(点击时加载)。
地图大小是移动设备中的完整显示大小! 我只是在弹出窗口中使用了 autoPanPadding 选项,但还不够
参考下图:
【问题讨论】:
标签: javascript dictionary leaflet
我希望我的标记在弹出窗口的中心居中.. 并且不在标记 latlng 中居中地图,而是在标记和弹出窗口的中心! 问题是弹出窗口具有动态内容(点击时加载)。
地图大小是移动设备中的完整显示大小! 我只是在弹出窗口中使用了 autoPanPadding 选项,但还不够
参考下图:
【问题讨论】:
标签: javascript dictionary leaflet
使用fitzpaddy 的回答,我能够使这段代码有效且更加灵活。
map.on('popupopen', function(e) {
var px = map.project(e.target._popup._latlng); // find the pixel location on the map where the popup anchor is
px.y -= e.target._popup._container.clientHeight/2; // find the height of the popup container, divide by 2, subtract from the Y axis of marker location
map.panTo(map.unproject(px),{animate: true}); // pan to new center
});
【讨论】:
Ciao Stefano,
这是未经测试的伪代码,但 Leaflet project/unproject 函数应该提供帮助。
即;
// Obtain latlng from mouse event
var latlng;
// Convert latlng to pixels
var px = project(latlng);
// Add pixel height offset to converted pixels (screen origin is top left)
px.y -= mypopup.height/2
// Convert back to coordinates
latlng = unproject(px);
// Pan map
map.panTo(latlng,{animate: true});
这取决于缩放比例在计算过程中保持不变,因此您可能需要平移地图,然后计算平移偏移以正确更新(使用animation,这将只是一个温和的过渡)。
祝你好运!
【讨论】:
这是一个简单的解决方案:
首先将地图以标记为中心。
map.setView(marker.latLng);
然后你打开弹出窗口。
var popup.openOn(map); = L.popup()
.setLatLng(marker.latLng)
.setContent(dynamic-content)
.openOn(map);
Leaflet 会自动平移地图,以便弹出窗口适合地图。要使其看起来更漂亮,您可以使用 CSS 为弹出窗口添加一个 margin-top。
【讨论】:
我非常简单的解决方案也保持当前缩放级别以提高可用性。
map.on('popupopen', function (e) {
map.setView(e.target._popup._latlng, e.target._zoom);
});
【讨论】: