feat(annotation): basic annotation page, not working yet

This commit is contained in:
Oliver
2025-05-19 17:11:17 +02:00
parent a64ef8d469
commit f2aa710a5e
4 changed files with 39 additions and 5 deletions

View File

@@ -10,12 +10,30 @@ import MapKit
struct AnnotationView: View { struct AnnotationView: View {
var pm: CLPlacemark var pm: CLPlacemark
var title: String?
var coordinate: CLLocation
@ObservedObject var viewModel: ViewModel
var body: some View { var body: some View {
ZStack { ZStack {
Rectangle() Rectangle()
.fill(.thinMaterial) .fill(.thinMaterial)
.ignoresSafeArea() .ignoresSafeArea()
VStack { VStack(alignment: .leading) {
HStack(alignment: .top) {
Text((title ?? pm.areasOfInterest?.first ?? pm.name) ?? "\(coordinate.coordinate.latitude.description)º, \(coordinate.coordinate.longitude.description)")
.font(.title)
.bold()
Spacer()
Button(action: {
viewModel.showDetails = false
}, label: {
Image(systemName: "multiply.circle")
})
}
.padding(.horizontal)
.padding(.top, 20)
Spacer()
Text(pm.locality ?? "") Text(pm.locality ?? "")
Text(pm.name ?? "") Text(pm.name ?? "")
Text("Name: \(pm.name ?? "")") Text("Name: \(pm.name ?? "")")
@@ -31,6 +49,8 @@ struct AnnotationView: View {
Image(systemName: Defaults.getIconFor(pointOfInterest: MKMapItem(placemark: mkPlacemark).pointOfInterestCategory)) Image(systemName: Defaults.getIconFor(pointOfInterest: MKMapItem(placemark: mkPlacemark).pointOfInterestCategory))
} }
} }
.frame(maxWidth: .infinity)
.ignoresSafeArea()
} }
} }
} }

View File

@@ -15,7 +15,6 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega
var viewModel: ViewModel var viewModel: ViewModel
var oldDirections: [MKRoute] = [] var oldDirections: [MKRoute] = []
var oldDestination: MKMapItemAnnotation? var oldDestination: MKMapItemAnnotation?
var detailsDisplayed = false
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
let searchViewConctroller: UIHostingController<SearchView> let searchViewConctroller: UIHostingController<SearchView>
var annotationViewController: UIHostingController<AnnotationView>? var annotationViewController: UIHostingController<AnnotationView>?
@@ -126,13 +125,13 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega
} }
func showAnnotation(annotation: MKAnnotation) { func showAnnotation(annotation: MKAnnotation) {
viewModel.showDetails = true
let location = CLLocation(latitude: annotation.coordinate.latitude, let location = CLLocation(latitude: annotation.coordinate.latitude,
longitude: annotation.coordinate.longitude) longitude: annotation.coordinate.longitude)
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
guard let placemark = placemarks?.first, error == nil else { return } guard let placemark = placemarks?.first, error == nil else { return }
print(placemark)
self.annotationViewController = UIHostingController(rootView: AnnotationView(pm: placemark)) self.annotationViewController = UIHostingController(rootView: AnnotationView(pm: placemark, title: annotation.title as? String, coordinate: location, viewModel: self.viewModel))
if let avc = self.annotationViewController { if let avc = self.annotationViewController {
avc.view.backgroundColor = .clear avc.view.backgroundColor = .clear
avc.modalPresentationStyle = .pageSheet avc.modalPresentationStyle = .pageSheet
@@ -156,7 +155,9 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega
} }
func mapView(_ mapView: MKMapView, didDeselect annotation: any MKAnnotation) { func mapView(_ mapView: MKMapView, didDeselect annotation: any MKAnnotation) {
showSearchView() if viewModel.showDetails {
viewModel.showDetails = false
}
} }
func hideSearchView() { func hideSearchView() {
@@ -175,6 +176,17 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega
self?.refreshRoute() self?.refreshRoute()
} }
.store(in: &cancellables) .store(in: &cancellables)
viewModel.$showDetails
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
print(value)
if !value {
self?.hideAnnotationView()
self?.showSearchView()
// self?.mapView.selectedAnnotations = []
}
}
.store(in: &cancellables)
} }
} }

View File

@@ -13,6 +13,8 @@ class ViewModel: ObservableObject {
@Published var directions: [MKRoute] = [] @Published var directions: [MKRoute] = []
@Published var stepLength: Double? @Published var stepLength: Double?
@Published var destination: MKMapItem? @Published var destination: MKMapItem?
@Published var showDetails = false
func saveValue(_ value: String) { func saveValue(_ value: String) {
UserDefaults.standard.set(value, forKey: "test") UserDefaults.standard.set(value, forKey: "test")