diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index fed2422..746a8c9 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -1661,7 +1661,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 165; + CURRENT_PROJECT_VERSION = 166; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = AutoCat; @@ -1690,7 +1690,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 165; + CURRENT_PROJECT_VERSION = 166; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = AutoCat; diff --git a/AutoCat/Screens/MapScreen/MapViewModel.swift b/AutoCat/Screens/MapScreen/MapViewModel.swift index 1bf7be5..139963e 100644 --- a/AutoCat/Screens/MapScreen/MapViewModel.swift +++ b/AutoCat/Screens/MapScreen/MapViewModel.swift @@ -75,6 +75,7 @@ final class MapViewModel: ACHudContainer { title = String.localizedStringWithFormat(NSLocalizedString("events found", comment: ""), events.count) await clusterManager.add(markers) await reloadMarkers() + currentRegion = getRegion(for: markers) } } @@ -112,4 +113,29 @@ final class MapViewModel: ACHudContainer { } } } + + func getRegion(for markers: [MapMarkerModel]) -> MKCoordinateRegion { + guard !markers.isEmpty else { + return .init() + } + + let latitudes = markers.map(\.coordinate.latitude) + let longitudes = markers.map(\.coordinate.longitude) + + let minLat = latitudes.min() ?? 0 + let maxLat = latitudes.max() ?? 0 + let minLong = longitudes.min() ?? 0 + let maxLong = longitudes.max() ?? 0 + + return .init( + center: .init( + latitude: (maxLat - minLat)/2, + longitude: (maxLong - minLong)/2 + ), + span: .init( + latitudeDelta: (maxLat - minLat)*1.2, + longitudeDelta: (maxLong - minLong)*1.2 + ) + ) + } }