Notification tap opens directions to the station
- Alerts (live + real-data test) carry the station name + coordinates in userInfo and attach a map snapshot with a red pin, so the expanded notification shows where the station is - Tapping a notification opens Apple Maps driving directions to the station from the user's current location (maps:// daddr) - If Apple Maps hand-off fails (LiveContainer), an in-app map sheet (StationMapView) pins the station + user location with a Directions button
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import SwiftUI
|
||||
import MapKit
|
||||
import UIKit
|
||||
|
||||
/// In-app fallback for notification taps that can't hand off to Apple Maps
|
||||
/// (e.g. inside LiveContainer). Shows the station on a map with the user's
|
||||
/// location, plus a button to open driving directions in Apple Maps.
|
||||
struct StationMapView: View {
|
||||
let request: StationMapRequest
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@State private var position: MapCameraPosition = .automatic
|
||||
|
||||
private var stationCoordinate: CLLocationCoordinate2D {
|
||||
CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Map(position: $position) {
|
||||
Marker(request.name, coordinate: stationCoordinate)
|
||||
.tint(.red)
|
||||
if let userLocation = FuelStore.loadLocation() {
|
||||
Annotation("You", coordinate: CLLocationCoordinate2D(
|
||||
latitude: userLocation.lat,
|
||||
longitude: userLocation.lng
|
||||
)) {
|
||||
Image(systemName: "location.circle.fill")
|
||||
.font(.title)
|
||||
.foregroundStyle(.blue)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle(request.name)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Done") { dismiss() }
|
||||
}
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button {
|
||||
openAppleMapsDirections()
|
||||
} label: {
|
||||
Label("Directions", systemImage: "arrow.triangle.turn.up.right.diamond.fill")
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
position = .region(MKCoordinateRegion(
|
||||
center: stationCoordinate,
|
||||
span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func openAppleMapsDirections() {
|
||||
let query = request.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
|
||||
guard let url = URL(string: "maps://?daddr=\(request.latitude),\(request.longitude)&q=\(query)") else { return }
|
||||
UIApplication.shared.open(url)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user