Map repositions itself after panning

Hi,


I am currently displaying multiple custom Annotations on a map and when i went up to about 250 the map starts lagging.

When i pan across the map: no problem. But as soon as the user stops panning the map repositions itself somewhere completely else.


I reset the annotations and removed

annotationView!.image = UIImage(named: "pin")

and everything was fine (off course the image was the tipical MapKit-logo but everything else worked out)


I am using a MKPointAnnotation class to define specific properties of my Annotations and this function to specify:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView {
     guard let annot = annotation as? customAnnotation else {return nil}
     
     if annotation is MKUserLocation { return nil }
     
     let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotation")
               
     mapView.addAnnotation(annot)
     annotationView.image = UIImage(named: "pin")
     annotationView.canShowCallout = true
     
     annotationView.clusteringIdentifier = String(describing: ClusterAnnotationView.self)

     //here just some Buttons inside the Callout
     
     return annotationView as MKAnnotationView


I am also using two methods to handle the amount of Annotation:

One of them deletes all annotations not shown on the screen and the other one clusters them ( -> clusteringIdentifier)


Thanks for your help,

Gus

Replies

It seems you end up adding annotations a lot of times by doing it in

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


That should probably be done in viewDidLoad.


Did you look at this tutorial to see how to implement all this ?

h ttps://www.raywenderlich.com/548-mapkit-tutorial-getting-started

But the annotations dont show up.


It adds annotations inside the visMapRect and then moves around to a different location without showing any annotations over there (they only load when i move the map a bit).


So the map adds annotations inside the Viewdid Load and just defines its properties and icon inside this mapView function


I tried around a little bit changing different types and realised that there really seems to be a problem with the icons.

when i set everything to MKMarkerAnnotations everything is fine. When i only change annotationView to MKMarkerAnnotationView it displays normal marker annotations with the clusters having customised icons and it starts laggin a bit. And when i leave everything as MKAnnotation so the annotations and clusters both have customised icons the problem (map jumping around) occurs.


Could there be a problem with handling different image formats?

You are not dequeuing annotations, but recreate them each time.


I think that could be the problem.


See here a code sample for MKMarkerAnnotations:


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

        var annotationView = MKMarkerAnnotationView()
        guard let annotation = annotation as? PizzaAnnotation else {return nil}

        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier") as? MKMarkerAnnotationView {
            annotationView = dequeuedView
        } else{
            annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
        }
        annotationView.markerTintColor = color
        annotationView.glyphImage = UIImage(named: "pizza")
        annotationView.glyphTintColor = .yellow
        annotationView.clusteringIdentifier = "identifier"
        return annotationView
    }

from h ttps://makeapppie.com/2018/02/20/use-markers-instead-of-pins-for-map-annotations/

still the same problem occuring


another thing i just realised is that sometimes the map automatically selects annotations (opening their callout)

maybe the map selects annotations which are somwhere else, so it zooms to their location?!


Also: when the map has moved itself to a different location, no annotations are displayed, so it doesnt realise that the region has changed (otherwise it would add annotations as soon as the panning has stopped)

Could you post your new code, after the modifications ?

Sure:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
     var annotationView MKAnnotationView()
     guard let annot = annotation as? customAnnotation else {return nil}

     if let dequeuedView  = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier") {
          annotationView = dequeuedView
     }
     else {
          annotationView = MKAnnotationView(annotation: annot, reuseIdentifier: "identifier")
     }
     
     annotationView.image = UIImage(named: "pin")
     annotationView.canShowCallout = true
     annotationView.clusteringIdentifier = "identifier"

     return annotationView
}


i again added some button after the clusteringIdentifier but i dont think they are the problem