diff --git a/Userland/Applications/Maps/MapWidget.cpp b/Userland/Applications/Maps/MapWidget.cpp index 1e576164f0..e11ac21d88 100644 --- a/Userland/Applications/Maps/MapWidget.cpp +++ b/Userland/Applications/Maps/MapWidget.cpp @@ -47,8 +47,16 @@ static double nice_round_number(double number) double MapWidget::LatLng::distance_to(LatLng const& other) const { - double const earth_radius = 6371000.0; - return earth_radius * 2.0 * asin(sqrt(pow(sin((AK::to_radians(other.latitude) - AK::to_radians(latitude)) / 2.0), 2.0) + cos(AK::to_radians(latitude)) * cos(AK::to_radians(other.latitude)) * pow(sin((AK::to_radians(other.longitude) - AK::to_radians(longitude)) / 2.0), 2.0))); + return EARTH_RADIUS * 2.0 * asin(sqrt(pow(sin((AK::to_radians(other.latitude) - AK::to_radians(latitude)) / 2.0), 2.0) + cos(AK::to_radians(latitude)) * cos(AK::to_radians(other.latitude)) * pow(sin((AK::to_radians(other.longitude) - AK::to_radians(longitude)) / 2.0), 2.0))); +} + +int MapWidget::LatLngBounds::get_zoom() const +{ + double distance_meters = north_west.distance_to(south_east); + int zoom = ZOOM_MIN; + while (distance_meters < EARTH_RADIUS / pow(2, zoom - 1) && zoom != ZOOM_MAX) + ++zoom; + return min(zoom + 1, ZOOM_MAX); } // MapWidget class diff --git a/Userland/Applications/Maps/MapWidget.h b/Userland/Applications/Maps/MapWidget.h index 6149a84370..b931e41175 100644 --- a/Userland/Applications/Maps/MapWidget.h +++ b/Userland/Applications/Maps/MapWidget.h @@ -27,6 +27,13 @@ public: double distance_to(LatLng const& other) const; }; + struct LatLngBounds { + LatLng north_west; + LatLng south_east; + + int get_zoom() const; + }; + struct Options { Optional tile_provider {}; LatLng center; @@ -141,6 +148,7 @@ private: static int constexpr TILE_SIZE = 256; static double constexpr LATITUDE_MAX = 85.0511287798066; + static int constexpr EARTH_RADIUS = 6371000.0; static size_t constexpr TILES_CACHE_MAX = 256; static constexpr size_t TILES_DOWNLOAD_PARALLEL_MAX = 8; static int constexpr ZOOM_MIN = 2;