Skip to content

Instantly share code, notes, and snippets.

@sfpgmr
Last active June 18, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfpgmr/99a3ce3f69791c9cc8997035caa10d0c to your computer and use it in GitHub Desktop.
Save sfpgmr/99a3ce3f69791c9cc8997035caa10d0c to your computer and use it in GitHub Desktop.
const pi = Math.PI;
const e = Math.E;
// tile座標とズームレベルから緯度経度を求める
function tile2latlon(x, y, z) {
const lon = (x / 2.0 ** z) * 360 - 180;// # 経度(東経)
const mapy = (y / 2.0 ** z) * 2 * pi - pi;
const lat = 2 * Math.atan(e ** (- mapy)) * 180 / pi - 90;// # 緯度(北緯)
return { lat: lat, lon: lon };
}
// 緯度経度からtile座標を求める
function latlon2tile(lat, lon, z) {
const x = ((lon / 180 + 1) * 2 ** z / 2) | 0;// # x座標
const y = (((-Math.log(Math.tan((45 + lat / 2) * pi / 180)) + pi) * 2 ** z / (2 * pi))) | 0;// # y座標
return { x: x, y: y };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment