Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Preview Camera and Preview Text #47

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
18 changes: 18 additions & 0 deletions OTPKit/Services/LocationManagerService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 5 additions & 6 deletions OTPKitDemo/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
.sheet(isPresented: isPlanResponsePresented, content: {
TripPlannerSheetView()
.presentationDetents([.medium, .large])
.interactiveDismissDisabled()

Check warning on line 71 in OTPKitDemo/MapView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
})
.sheet(isPresented: isStepsViewPresented, onDismiss: {
locationManagerService.resetTripPlanner()
Expand All @@ -83,18 +84,16 @@

if locationManagerService.isFetchingResponse {
ProgressView()
}

if locationManagerService.isMapMarkingMode {
} else if locationManagerService.isMapMarkingMode {
MapMarkingView()

} else if locationManagerService.selectedItinerary != nil,
locationManagerService.isStepsViewPresented == false {
locationManagerService.isStepsViewPresented == false
{

Check warning on line 91 in OTPKitDemo/MapView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration (opening_brace)
VStack {
Spacer()
TripPlannerView()
}
} else {
} else if locationManagerService.planResponse == nil, locationManagerService.isStepsViewPresented == false {
VStack {
Spacer()
OriginDestinationView()
Expand Down
Loading