MapKit Annotations Disappearing

前端 未结 5 1517
囚心锁ツ
囚心锁ツ 2020-12-31 09:08

I have an array of latitudes and another array of longitudes that I add to an array of type CLLocationCoordinate2D. I then use the new array to annotate multiple points on t

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 09:50

    I was experiencing a similar issue. My best guess is that it has something to do with how iOS 11 detects pin collisions. Implementing a custom annotation view or reverting to use the iOS 10 pin fixed the problem for me.

    For example, implementing the following should fix your code:

    class MultiMapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
       override func viewDidLoad() {
           super.viewDidLoad()
           mapView.delegate = self    
        }
    
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
           guard let annotation = annotation as? MKPointAnnotation else { return nil }
    
           let identifier = "pin-marker"
           var view: MKAnnotationView
    
           if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
               dequeuedView.annotation = annotation
               view = dequeuedView
           } else {
               view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
           }
           return view
       }
    }
    

    If this doesn't work, there is a displayPriority property that is worth looking into as it is responsible for helping to determine when pins should be hidden/shown at different zoom levels. More info at https://developer.apple.com/documentation/mapkit/mkannotationview/2867298-displaypriority

    Hope this helps.

提交回复
热议问题