diff --git a/OTPKit/Features/TripPlanner/SelectItenerary/TripPlannerSheetView.swift b/OTPKit/Features/TripPlanner/SelectItenerary/TripPlannerSheetView.swift index 8405d0e..5199f11 100644 --- a/OTPKit/Features/TripPlanner/SelectItenerary/TripPlannerSheetView.swift +++ b/OTPKit/Features/TripPlanner/SelectItenerary/TripPlannerSheetView.swift @@ -62,13 +62,13 @@ public struct TripPlannerSheetView: View { Button(action: { locationManagerService.selectedItinerary = itinerary locationManagerService.planResponse = nil + locationManagerService.adjustOriginDestinationCamera() dismiss() }, label: { - Text("Go") + Text("Preview") .padding(30) .background(Color.green) .foregroundStyle(.foreground) - .font(.title) .fontWeight(.bold) .clipShape(RoundedRectangle(cornerRadius: 12)) }) diff --git a/OTPKit/Services/LocationManagerService.swift b/OTPKit/Services/LocationManagerService.swift index 2209bf6..a588e61 100644 --- a/OTPKit/Services/LocationManagerService.swift +++ b/OTPKit/Services/LocationManagerService.swift @@ -177,6 +177,24 @@ public final class LocationManagerService: NSObject, ObservableObject { return MapPolyline(coordinates: coordinates) } + public func adjustOriginDestinationCamera() { + guard let originCoordinate, let destinationCoordinate else { return } + // Create a rectangle that encompasses both coordinates + let minLat = min(originCoordinate.latitude, destinationCoordinate.latitude) + let maxLat = max(originCoordinate.latitude, destinationCoordinate.latitude) + let minLon = min(originCoordinate.longitude, destinationCoordinate.longitude) + let maxLon = max(originCoordinate.longitude, destinationCoordinate.longitude) + + let center = CLLocationCoordinate2D(latitude: (minLat + maxLat) / 2, + longitude: (minLon + maxLon) / 2) + let span = MKCoordinateSpan(latitudeDelta: (maxLat - minLat) * 1.5, + longitudeDelta: (maxLon - minLon) * 1.5) + + let region = MKCoordinateRegion(center: center, span: span) + + currentCameraPosition = .region(region) + } + // MARK: - Trip Planner Methods private func checkAndFetchTripPlanner() { diff --git a/OTPKitDemo/MapView.swift b/OTPKitDemo/MapView.swift index 677e67f..001c09e 100644 --- a/OTPKitDemo/MapView.swift +++ b/OTPKitDemo/MapView.swift @@ -68,6 +68,7 @@ public struct MapView: View { .sheet(isPresented: isPlanResponsePresented, content: { TripPlannerSheetView() .presentationDetents([.medium, .large]) + .interactiveDismissDisabled() }) .sheet(isPresented: isStepsViewPresented, onDismiss: { locationManagerService.resetTripPlanner() @@ -83,18 +84,16 @@ public struct MapView: View { if locationManagerService.isFetchingResponse { ProgressView() - } - - if locationManagerService.isMapMarkingMode { + } else if locationManagerService.isMapMarkingMode { MapMarkingView() - } else if locationManagerService.selectedItinerary != nil, - locationManagerService.isStepsViewPresented == false { + locationManagerService.isStepsViewPresented == false + { VStack { Spacer() TripPlannerView() } - } else { + } else if locationManagerService.planResponse == nil, locationManagerService.isStepsViewPresented == false { VStack { Spacer() OriginDestinationView()