-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[not verified] Add util functions to convert from/between Mapbox zoom…
… 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
1 parent
cee144a
commit c7b4a08
Showing
2 changed files
with
21 additions
and
0 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/add-map-block-mapkit-zoom-conversion
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
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 |
17 changes: 17 additions & 0 deletions
17
projects/plugins/jetpack/extensions/blocks/map/mapkit-utils/index.js
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
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 }; |