【问题标题】:Get the bounding box of the visible leaflet map?获取可见传单地图的边界框?
【发布时间】:2014-04-08 21:01:54
【问题描述】:

我有一张可以平移和缩放的传单地图。如何动态获取地图边缘的纬度/经度(放大/缩小+平移后)?

【问题讨论】:

  • 您是指地图的边界/范围吗?还是围绕范围边界的每个纬度?如果您只想获取边界,可以在 dragend 上调用 map.getBounds()。 map.on('dragend', myFunctionThatGetsMapBounds()) 每次移动地图时,我都会尝试这样的方法来获取边界。 zoomend 也可以这样做。

标签: leaflet


【解决方案1】:

你可能猜到了:

map.getBounds();

如果你想在平移和缩放后得到边界,使用events,like

map.on('moveend', function() { 
     alert(map.getBounds());
});

【讨论】:

【解决方案2】:

上述答案是正确的,但不是很有帮助,不幸的是,传单文档没有提供有关 getBounds() 调用的详细信息。

如果你把它添加到你的地图初始化函数中,你可以看到地图数据显示:

map.on('dragend', function onDragEnd(){
    var width = map.getBounds().getEast() - map.getBounds().getWest();
    var height = map.getBounds().getNorth() - map.getBounds().getSouth();

    alert (
        'center:' + map.getCenter() +'\n'+
        'width:' + width +'\n'+
        'height:' + height +'\n'+
        'size in pixels:' + map.getSize()
    )});

请注意,size 是地图窗口的像素尺寸,其中 bounds 是缩放的内部尺寸。尝试缩放,您会看到大小保持不变,但每个缩放级别的高度和两倍。

在 LatLngBounds 对象下有一大堆方法,因此您应该能够找到其中一个可以为您提供所需信息的调用。

【讨论】:

  • 前一个答案很简洁。
猜你喜欢
  • 1970-01-01
  • 2016-04-06
  • 2014-01-12
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
相关资源
最近更新 更多