Javascript 在传单地图中偏移LatLngBounds
我有一个填充屏幕的地图,屏幕底部显示了非地图内容的水平叠加。我希望在地图上显示一条多段线,使其在地图视图中尽可能大,但不隐藏在覆盖内容下方 下面是我正在尝试的,它几乎可以工作,但根据地图当前视图的缩放/位置,会给出不同的结果。我需要一些独立于当前地图视图的东西Javascript 在传单地图中偏移LatLngBounds,javascript,maps,leaflet,Javascript,Maps,Leaflet,我有一个填充屏幕的地图,屏幕底部显示了非地图内容的水平叠加。我希望在地图上显示一条多段线,使其在地图视图中尽可能大,但不隐藏在覆盖内容下方 下面是我正在尝试的,它几乎可以工作,但根据地图当前视图的缩放/位置,会给出不同的结果。我需要一些独立于当前地图视图的东西 // `map` is the leaflet map // `polyline` is a leaflet polyline function fitBounds (latLngBounds, offsetY) { // offse
// `map` is the leaflet map
// `polyline` is a leaflet polyline
function fitBounds (latLngBounds, offsetY) { // offsetY in pixels
var zoom, southeast, southeastOffset, newBounds;
if (offsetY) {
zoom = map.getBoundsZoom(latLngBounds);
southeast = map.project(latLngBounds.getSouthEast(), zoom);
southeastOffset = new L.Point(southeast.x, southeast.y + offsetY);
newBounds = latLngBounds.extend(map.unproject(southeastOffset, zoom));
}
map.fitBounds(newBounds || latLngBounds);
}
var contentHeight = 350;
// this will be calculated and is the distance from the top of the
// overlaid content to the bottom of the map
fitBounds(polyline.getBounds(), contentHeight);
当对地图进行不同的平移或缩放时,map.getBoundsZoom(latLngBounds)
和项目
/取消项目
似乎返回不同的值。我从中了解到,它们将独立于当前的地图视图
我使用的方法是错误的,还是有不同的策略来实现我所需要的?谢谢。
getBoundsZoom
取决于当前地图视图端口大小。因此,如果您使用不同的大小进行测试(例如,您的地图容器填充整个页面宽度,并且您的浏览器窗口宽度不同,可能是因为控制台),结果将不同
如果您确定地图容器大小没有改变,并且可以在JSBin/Plunker/JSFiddle上重现问题,那么传单中可能有一个bug;请随时在问题跟踪器中报告。查看
传单活动区域
和传单。位于的Controlled Bounds
插件,它们以非常类似的方式实施策略来装饰对fitBounds()
的调用。感谢IvanSanchez。我已成功获得传单活动区域
处理该问题,从而解决了我的问题。如果有人能看到它并想发表评论,我仍然对原始代码不起作用的原因感兴趣。谢谢。这可能就是问题所在。