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 {
var pm: CLPlacemark
var title: String?
var coordinate: CLLocation
@ObservedObject var viewModel: ViewModel
var body: some View {
ZStack {
Rectangle()
.fill(.thinMaterial)
.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.name ?? "")
Text("Name: \(pm.name ?? "")")
@@ -31,6 +49,8 @@ struct AnnotationView: View {
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 oldDirections: [MKRoute] = []
var oldDestination: MKMapItemAnnotation?
var detailsDisplayed = false
private var cancellables = Set<AnyCancellable>()
let searchViewConctroller: UIHostingController<SearchView>
var annotationViewController: UIHostingController<AnnotationView>?
@@ -126,13 +125,13 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega
}
func showAnnotation(annotation: MKAnnotation) {
viewModel.showDetails = true
let location = CLLocation(latitude: annotation.coordinate.latitude,
longitude: annotation.coordinate.longitude)
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
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 {
avc.view.backgroundColor = .clear
avc.modalPresentationStyle = .pageSheet
@@ -156,7 +155,9 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega
}
func mapView(_ mapView: MKMapView, didDeselect annotation: any MKAnnotation) {
showSearchView()
if viewModel.showDetails {
viewModel.showDetails = false
}
}
func hideSearchView() {
@@ -175,6 +176,17 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega
self?.refreshRoute()
}
.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 stepLength: Double?
@Published var destination: MKMapItem?
@Published var showDetails = false
func saveValue(_ value: String) {
UserDefaults.standard.set(value, forKey: "test")