Skip to content

Instantly share code, notes, and snippets.

@stepankuzmin
Created May 7, 2025 09:33
Show Gist options
  • Save stepankuzmin/6e3fccf4c7914972073d8e8e2900e1bb to your computer and use it in GitHub Desktop.
Save stepankuzmin/6e3fccf4c7914972073d8e8e2900e1bb to your computer and use it in GitHub Desktop.
Get tile bounds from XYZ
export function lngFromMercatorX(x: number): number {
return x * 360 - 180;
}
export function latFromMercatorY(y: number): number {
const y2 = 180 - y * 360;
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
}
function getBounds(tileID: CanonicalTileID): [number, number, number, number] {
const worldSize = Math.pow(2, tileID.z);
const x1 = tileID.x / worldSize;
const x2 = (tileID.x + 1) / worldSize;
const y1 = tileID.y / worldSize;
const y2 = (tileID.y + 1) / worldSize;
// left, bottom, right, top
return [
lngFromMercatorX(x1),
latFromMercatorY(y2),
lngFromMercatorX(x2),
latFromMercatorY(y1)
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment