TigrouMeow
TigrouMeow

Reputation: 3779

Detect when all the annotations have been successfully been drawn or added to MKMapView

I posted another question about MKMapView today. The code is actually the same one, so if you need to see it, it's here: How to get the MKMapView instance from my UIViewRepresentable view?.

In the updateUIView, I am adding annotations when there is a difference. It's pretty basic and it looks like this:

if view.annotations.count != annotations.count {
  DispatchQueue.main.async {
    view.removeAnnotations(view.annotations)
    view.addAnnotations(annotations)
  }
}

I am not sure it's the best way to do this, but it works (I should probably do a diff between the two arrays and only add the relevant and new annotations, but I am a beginner with SwiftUI so I am doing all this little by little - if you have a logical solution, I am interested as well).

The process of adding/creating annotations take a long time (especially the first time, maybe because I generated images for each of them in my coordinator) so I would like to display a spinner while they are being created (how to create a spinner is not the problem). My problem is to know when to toggle off this spinner.

I have tried a few functions, like this one:

func mapViewDidFinishRenderingMap(_ mapView: MKMapView, fullyRendered: Bool) {
  // print("[Coordinator] finishRendering: " + String(fullyRendered))
}

And a lot of others, but none are working, they always get called before the annotations are actually displayed.

Another issue I have is that the addAnnotation seems to freeze the UI, even though the process is asynchronous. I am building the annotations through this function:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

I am a bit lost with what to share or not, so I hope that will be enough. I am still a beginner with SwiftUI and trying to figure out a lot of things.

Thanks for your help :)

Upvotes: 0

Views: 99

Answers (0)

Related Questions