Skip to content

Instantly share code, notes, and snippets.

@duzenko
Created May 29, 2020 15:37
Show Gist options
  • Save duzenko/9e71c5595b05a07f0d0294d49c03e8fb to your computer and use it in GitHub Desktop.
Save duzenko/9e71c5595b05a07f0d0294d49c03e8fb to your computer and use it in GitHub Desktop.
Web Mercator projection (e.g. ImageMapTypeOptions.getTitleUrl)
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