【问题标题】:How to get coords of tiles that are currently visible?如何获取当前可见的瓷砖坐标?
【发布时间】:2014-07-22 18:36:55
【问题描述】:

我需要获取当前可见图块的坐标x/y(其中 x 和 y 是 Leaflet.js 用来查询对应 url 的坐标,x/y/z 用于图块)。

我想在不启用unloadInvisibleTiles 选项的情况下找到解决方案。

唯一的方法是通过getPixelBounds()吗?

编辑: 添加了一个示例gist

【问题讨论】:

    标签: javascript maps leaflet


    【解决方案1】:

    据我所知,你必须通过getPixelBounds()

    您可以使用以下代码枚举它们:xTile 和 yTile 是您要查找的内容

    // get bounds, zoom and tileSize        
    var bounds = map.getPixelBounds();
    var zoom = map.getZoom();
    var tileSize = 256;  
    
    
    // get NorthWest and SouthEast points
    var nwTilePoint = new L.Point(Math.floor(bounds.min.x / tileSize),
        Math.floor(bounds.min.y / tileSize));
    
    var seTilePoint = new L.Point(Math.floor(bounds.max.x / tileSize),
        Math.floor(bounds.max.y / tileSize));
    
    // get max number of tiles in this zoom level
    var max = map.options.crs.scale(zoom) / tileSize; 
    
    // enumerate visible tiles 
    for (var x = nwTilePoint.x; x <= seTilePoint.x; x++) {
       for (var y = nwTilePoint.y; y <= seTilePoint.y; y++) {
    
          var xTile = (x + max) % max;
          var yTile = (y + max) % max;
    
          console.log('tile ' + xTile + ' ' + yTile);
        }
    }
    

    【讨论】:

    • 很棒的代码!谢谢!但是有一个极端情况。将其设为 Math.abs(x%max) 和 Math.abs(y%max) 否则您有时会出现负平铺坐标。
    • 实际上,x 和 y 在墨卡托投影中始终为正:wiki.openstreetmap.org/wiki/Slippy_map_tilenames#X_and_Y
    • 是的,你是对的,但是如果你在地图开始重复时向左滚动地图,getPixelBounds() 会返回负数。
    • 方法getPixelBounds 似乎在最新版本中没有出现(version: "0.37.0").. 所以这个答案已被弃用。有什么解决方法吗?
    • 你包括什么?当前版本的传单是1.0.3,方法还在:leafletjs.com/reference-1.0.3.html#map-getpixelbounds.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多