From c5b343fbb89515f49a56a2de7122068c2862fef7 Mon Sep 17 00:00:00 2001 From: Tim Broddin Date: Wed, 22 Feb 2023 16:09:08 +0100 Subject: [PATCH] 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 --- .../add-map-block-mapkit-zoom-conversion | 4 ++++ .../extensions/blocks/map/mapkit-utils/index.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 projects/plugins/jetpack/changelog/add-map-block-mapkit-zoom-conversion create mode 100644 projects/plugins/jetpack/extensions/blocks/map/mapkit-utils/index.js 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 };