Last active
October 10, 2019 10:02
-
-
Save iamvdo/0001c0c172ddc5b8a66a96c39c7a7ffd to your computer and use it in GitHub Desktop.
Geo functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get Lat/Lng from Tile x,y,z | |
// (returns top/left corner of tile) | |
function tile2latlng (x, y, z) { | |
const n = Math.pow(2, z); | |
const lat = Math.atan(Math.sinh(Math.PI * (1 - 2 * y / n))) * 180 / Math.PI; | |
const lng = x / n * 360 - 180; | |
return { lat, lng }; | |
} | |
// Get Tile x,y from Lat/Lng,z | |
function latlng2tile (lat, lng, z) { | |
const n = Math.pow(2, z); | |
const x = Math.floor((lng + 180) / 360 * n); | |
const y = Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * n); | |
return { x, y }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment