如何在 Mapbox GL JS 中获取当前图块的 X 和 Y 索引?

How to get X and Y indexes of the current tile in Mapbox GL JS?

我正在使用 Mapbox GL JS 使用我自己的图块服务器显示地图(它只提供 Z/X/Y 目录结构中的 openmaptiles pbf 文件)。

瓦片的加载是根据当前的中心坐标自动完成的。

我想为离线解决方案预加载周围的图块。 我知道如何使用 map.getZoom()

获取当前缩放级别

如何获取当前图块的 X 和 Y 索引 URL?

我建议用地理坐标来界定问题:

  • 您可以从 Map#getBounds)
  • 获取视口地理坐标
  • 您可以使用一些light math
  • 将地理坐标转换为图块坐标

此外,如果您需要在 bash 脚本中进行一些计算,您可以使用 mercantile library or CLI utility to transform lon/lan coordinates to tiles, find parent or children tiles and get some other information. Another advanced tool is supermercado