Untitled
unknown
plain_text
a month ago
3.0 kB
2
Indexable
Never
// // MapView.swift // RooftopCoffee // // Created by Bình Ngô on 2/8/24. // import SwiftUI import MapKit struct CustomMap: UIViewRepresentable { class Coordinator: NSObject, MKMapViewDelegate { var parent: CustomMap init(parent: CustomMap) { self.parent = parent } func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { guard let title = view.annotation?.title ?? "" else { return } parent.annotationTitle = title parent.showingPopup = true } } @Binding var region: MKCoordinateRegion @Binding var showingPopup: Bool @Binding var annotationTitle: String func makeCoordinator() -> Coordinator { return Coordinator(parent: self) } func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() mapView.delegate = context.coordinator return mapView } func updateUIView(_ uiView: MKMapView, context: Context) { uiView.setRegion(region, animated: true) uiView.removeAnnotations(uiView.annotations) let annotation = MKPointAnnotation() annotation.coordinate = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194) // Example coordinates annotation.title = "San Francisco" uiView.addAnnotation(annotation) } } struct MapView: View { var coffeeShop: CoffeeShop var region: MKCoordinateRegion var coordinate: CLLocationCoordinate2D var zoomLevel = CLLocationDegrees(0.04) init(coffeeShop: CoffeeShop) { self.coffeeShop = coffeeShop self.coordinate = CLLocationCoordinate2D( latitude: coffeeShop.location.lat, longitude: coffeeShop.location.lng) self.region = MKCoordinateRegion( center: self.coordinate, span: MKCoordinateSpan( latitudeDelta: zoomLevel, longitudeDelta: zoomLevel)) } var body: some View { ZStack { CustomMap(region: region, showingPopup: $showingPopup, annotationTitle: $annotationTitle) .edgesIgnoringSafeArea(.all) } .frame(width: 390, height: 500) // Map(initialPosition: .region(region)) { // Annotation(coffeeShop.name, // coordinate: self.coordinate) { // ZStack { // RoundedRectangle(cornerRadius: 5) // .fill(Color(UIColor(named: "MyPrimary")!)) // Image(systemName: "wineglass.fill") // .renderingMode(.original) // .foregroundColor(Color.white) // .padding(5) // } // } // } // .mapControlVisibility(.hidden) // .frame(width: 390, height: 500) } } #Preview { MapView(coffeeShop: CoffeeShopModel.coffeeShopList[0]) }
Leave a Comment