有没有办法确保在执行 fitbounds 后打开的弹出窗口在视图内

Is there a way to ensure an open popup is within the view after doing fitbounds

所以我有太多点: a = 固定位置,打开弹出窗口 "Destination" b = 用户当前位置,无弹出窗口

我知道我可以很容易地打电话(概念上) map.fitbound([latlng of a, latlng of b]) 这将缩放我的地图以包含这两个点,问题是它没有考虑打开的弹出窗口。由于我不知道用户的位置、定位点的方向或距离,因此我认为不可能使用填充。

关于如何解决这个问题的任何提示?

这里有一个 fiddle 演示了这个问题。

我看过几篇关于居中弹出窗口的帖子,但是 javascript 那里有点关于我,js2coffee 转换器不喜欢它。

I don't think that using padding is possible

Yes, yes it is,例如:

  map.fitBounds(someLatLngBounds, {padding: [100, 100]});

看到一个working demo, and don't forget to read the docs

您可以探索无需移动地图即可始终显示弹出窗口的解决方案。

例如:https://github.com/erictheise/rrose

以下是我的使用方法:http://franceimage.github.io/map

移动地图使标记靠近边缘,悬停或单击它,弹出窗口始终可见。

让弹出窗口每次出现在相同的位置

我正在使用以下 CSS 解决方案来修复弹出窗口 总是出现在我所在的同一个地方想要

<style>
    .leaflet-popup {
        transform: none !important;
        position: absolute;
        top: 600px !important;
        left: 20px !important;
    }
</style>

然后为了防止每次单击标记时地图移动,请在弹出选项中将 autoPan 设置为 false (https://leafletjs.com/reference-1.6.0.html#popup-autopan) :

 { autoPan: false }