Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util functions to convert from/between Mapbox zoom levels & Mapkit camera distance #29092

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Adds util functions for mapkit that allow converting zoom levels to camera distance, and back
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const earthRadius = 6.371e6;

function getMetersPerPixel( latitude ) {
return Math.abs( ( earthRadius * Math.cos( ( latitude * Math.PI ) / 180 ) * 2 * Math.PI ) / 256 );
}

function convertZoomLevelToCameraDistance( zoomLevel, latitude ) {
const altitude = ( 512 / Math.pow( 2, zoomLevel ) ) * 0.5; // altitude in pixels
return altitude * getMetersPerPixel( latitude );
}

function convertCameraDistanceToZoomLevel( cameraDistance, latitude ) {
const altitude = cameraDistance / getMetersPerPixel( latitude );
return Math.log2( 512 / ( altitude / 0.5 ) );
}

export { convertZoomLevelToCameraDistance, convertCameraDistanceToZoomLevel };