Skip to content

Commit

Permalink
[not verified] Add util functions to convert from/between Mapbox zoom…
Browse files Browse the repository at this point in the history
… levels & Mapkit 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 committed Feb 24, 2023
1 parent cee144a commit c7b4a08
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 c7b4a08

Please sign in to comment.