Last active
June 17, 2020 15:14
-
-
Save aataraxiaa/cbdb08f00348e2cc0c9d760c0a573796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| import MapKit | |
| // Main view | |
| struct ContentView: View { | |
| // Provides an annotation after some delay (asynchronously) | |
| @ObservedObject var asynchronousAnnotationMaker = AsynchronousAnnotationMaker() | |
| var body: some View { | |
| MapView(annotation: asynchronousAnnotationMaker.annotation).edgesIgnoringSafeArea(.all) | |
| .onAppear { | |
| self.asynchronousAnnotationMaker.makeAnnotation() | |
| } | |
| } | |
| } | |
| // Map view. Displays a map and a single map annotation. | |
| struct MapView: UIViewRepresentable { | |
| var annotation: MKAnnotation? | |
| func makeUIView(context: Context) -> MKMapView { | |
| return MKMapView() | |
| } | |
| func updateUIView(_ mapView: MKMapView, context: Context) { | |
| refreshAnnotation(onMap: mapView) | |
| } | |
| private func refreshAnnotation(onMap mapView: MKMapView) { | |
| mapView.removeAnnotations(mapView.annotations) | |
| if let annotation = annotation { | |
| mapView.addAnnotation(annotation) | |
| } | |
| } | |
| } | |
| // Provides a map annotation afte some delay. A timer is used to simulate a delay which may occur due to a network request | |
| class AsynchronousAnnotationMaker: ObservableObject { | |
| @Published var annotation: MKAnnotation? | |
| func makeAnnotation() { | |
| Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { timer in | |
| self.annotation = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 53.345589, longitude: -6.286951), addressDictionary: ["City":"Dublin", "Country":"Ireland"]) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment