Skip to content

Commit

Permalink
Add util functions to convert from/between Mapbox zoom levels & Mapki…
Browse files Browse the repository at this point in the history
…t camera distance (#29092)

* Add helper functions to convert from & to zoom level

* Add helper functions to convert from & to zoom level

* Simplified convertCameraDistanceToZoomLevel
  • Loading branch information
TimBroddin authored Feb 22, 2023
1 parent 68f8cb6 commit c5b343f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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 };

0 comments on commit c5b343f

Please sign in to comment.