Created
May 29, 2020 15:37
-
-
Save duzenko/9e71c5595b05a07f0d0294d49c03e8fb to your computer and use it in GitHub Desktop.
Web Mercator projection (e.g. ImageMapTypeOptions.getTitleUrl)
This file contains 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
const project = ( latLng: google.maps.LatLng, zoom: number ) => { | |
const scale = Math.pow( 2, zoom ); | |
const lambda = latLng.lng() / 180 * Math.PI; | |
const phi = latLng.lat() / 180 * Math.PI; | |
return new google.maps.Point( | |
256 / 2 / Math.PI * scale * ( lambda + Math.PI ), | |
256 / 2 / Math.PI * scale * ( Math.PI - Math.log( Math.tan( Math.PI / 4 + phi / 2 ) ) ) | |
); | |
} | |
const unproject = ( point: google.maps.Point, zoom: number ) => { | |
const scale = Math.pow( 2, zoom ); | |
const lambda = point.x / ( 256 / 2 / Math.PI * scale ) - Math.PI; | |
const phi = ( Math.atan( Math.exp( Math.PI - point.y / ( 256 / 2 / Math.PI * scale ) ) ) - Math.PI / 4 ) * 2; | |
return new google.maps.LatLng( | |
phi / Math.PI * 180, | |
lambda / Math.PI * 180 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment