MapKit Annotations - want ALL to show

Hey everyone,
I have a question around showing all plots on a map at the same time. I have an app that uses MapKit and everything works, but not how I want it to. When I initialize the map, my current location shows up and one plot pin. The issue is my array consists of many different locations, not just the one. When I zoom in a little, more show up, but when I zoom out, they start disappearing. I can move my map a little and more will come into focus as well. My MKCoordinateSpan is set to 0.01 initially, and I don’t want to change that. I have also set the displayPriority to .required as I loop through my records. All code executes as I go through and debug, at least it hits each step without any issues. Any thoughts on this would be greatly appreciated. I have been trying the various google recommendations, but none seem to be fixing the issue.

Thanks - Mike

Hi Mike,

What you are seeing is normal. If you zoom out and the pins get closer together MapKit will group them into one ‘visually’ and then as you zoom back in the individual pins will re-appear.

1 Like

Hi Chris, I agree with you that that is the default action. The issue I was having is that only one or two pins were showing up no matter how far I zoomed in. I corrected this, I guess, by creating my own pin image and then adding the following function and extension to my view controller:

func createAnnotations(locations: Results<MyFish>) {
    
    for location in locations {
        if location.lat != 0 && location.lon != 0 {
            let annotations = MKPointAnnotation()
            annotations.title = location.species
            annotations.coordinate = CLLocationCoordinate2D(latitude: location["lat"] as! CLLocationDegrees , longitude: location["lon"] as! CLLocationDegrees )
            
            mapView.addAnnotation(annotations)
            
        }
    }
}

extension MapViewController: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotations: MKAnnotation) -> MKAnnotationView? {
    
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
    
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotations, reuseIdentifier: "AnnotationView")
    }
    
    if  let title = annotations.title, title == "Walleye" {
        annotationView?.image = UIImage(named: "redPin")
    } else if let title = annotations.title, (title?.contains("Bass"))!  {
        annotationView?.image = UIImage(named: "greenPin")
    } else if let title = annotations.title, title == "Muskellunge"  {
        annotationView?.image = UIImage(named: "purplePin")
    } else if let title = annotations.title, title == "Northern Pike"  {
        annotationView?.image = UIImage(named: "purplePin")
    } else {
        annotationView?.image = UIImage(named: "yellowPin")
    }
    
    annotationView?.canShowCallout = true
    
    return annotationView
}

}

now all pins show up within my region…

Mike

That’s a good result Mike. Well done.