通过一些填充或比率缩小传单地图的边界

Shrink leaflet map's bounds by some padding or ratio

我想将 Leaflet 地图的边界缩小一些因素(比如 50px5%)。

我试过以下方法:

marginPoint = new L.Point(50, 50);
bounds = map.getPixelBounds();
bottomLeft = bounds.getBottomLeft();
topRight = bounds.getTopRight();

bounds = new L.LatLngBounds(
  map.unproject(bottomLeft.subtract(marginPoint)),
  map.unproject(topRight.subtract(marginPoint))
);

但是,这不会缩小地图,它只是moves/translates当前框/当前地图的边界。

由于您 subtract 到两个地图容器的角的数量相同,结果是您只是将地图视图移动到西南(左下角)。

如果您尝试 "zoom in"(即地图显示地理区域的较小部分,但在相同的容器尺寸内),您可以在左下角添加一些像素,并删除一些你的左上角。

但请注意,您也可以简单地使用 latLngBounds.pad() method(带有一个否定的 bufferRatio 参数)来获得相同的结果。