如何获得当前可见的瓦片的坐标

How to get coords of tiles that are currently visible?

本文关键字:坐标 何获得      更新时间:2023-09-26

我需要获得x/y(其中x和y是leafet .js使用x/y/z查询相应url的坐标)当前可见的坐标。

我想找到一个不启用unloadInvisibleTiles选项的解决方案。

只有通过getPixelBounds()才能做到这一点吗?

编辑:添加了一个示例gist

据我所知,你必须通过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);
    }
}