diff --git a/projects/plugins/jetpack/changelog/add-map-block-mapkit-zoom-conversion b/projects/plugins/jetpack/changelog/add-map-block-mapkit-zoom-conversion new file mode 100644 index 0000000000000..955697ec9630a --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-map-block-mapkit-zoom-conversion @@ -0,0 +1,4 @@ +Significance: minor +Type: other + +Adds util functions for mapkit that allow converting zoom levels to camera distance, and back diff --git a/projects/plugins/jetpack/extensions/blocks/map/mapkit-utils/index.js b/projects/plugins/jetpack/extensions/blocks/map/mapkit-utils/index.js new file mode 100644 index 0000000000000..96db0dd7b3a10 --- /dev/null +++ b/projects/plugins/jetpack/extensions/blocks/map/mapkit-utils/index.js @@ -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 };