0

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 :)

TigrouMeow
  • 3,669
  • 3
  • 27
  • 31
  • I am not sure you can. The `MapView` in the coordinator, is no different than any designed for UIKit, so those answers would apply. However, my suggestion would be to use a placeholder image, and then update the annotations as the images are computed. The important thing is the speed and suppleness of the UI. Rather than have a spinner saying "this is slow, wait", have it react immediately, and then do housekeeping later. – Yrb May 19 '22 at 12:39
  • @Yrb For me, the only goal is to avoid having an UI that is completely frozen for 2-5 seconds without showing that something is going on :) Or maybe I am doing something else wrong and adding annotations shouldn't freeze the UI? I don't really see how I could implement a placeholder for those annotations, you mean I should do that and lazily add the image in the placeholder? In fact, it's an UIImage has I build myself by simply mixing a Circle and an icon (an image which is part of the resources). – TigrouMeow May 21 '22 at 00:23

0 Answers0