Abdulla Jafar
Abdulla Jafar

Reputation: 31

Custom Apple Map Annotations not working properly

What I'm trying to do is to display a custom annotation view on a map view (MKMapView), that is working fine in the app. The problem is when I'm switching between apps or changing between dark & light modes the custom annotation view seems to revert to the default annotation pin as shown below.

Deployment Target: iOS 11

Running on: iPhone XS MAX iOS 13.3

Xcode Version: 11.3 (11C29)

Swift Version: 5.0

    private func addAnnotations(_ places : [QPPlaceDM]) {
        mapView.removeAnnotations(myAnnotations)
        var annotations = [MKPointAnnotation]()
        for item in places {
            let annotation = QPCustomAnnotaion()
            annotation.title = item.name
            annotation.coordinate = item.coordinates
            annotation.image = item.category.pinImage
            annotations.append(annotation)
        }
        myAnnotations = annotations
        mapView.addAnnotations(myAnnotations)
    }

.......

extension QPMainVC  : MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        
        if !(annotation is QPCustomAnnotaion) {
            return nil
        }
        let annotationID = "AnnotationId"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationID)

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationID)
            annotationView?.canShowCallout = true
        }
        else {
            annotationView?.annotation = annotation
        }


        let myCustomAnnotation = annotation as? QPCustomAnnotaion
        annotationView?.image = myCustomAnnotation?.image

        annotationView?.displayPriority = .required

        return annotationView
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        guard let annotationLocation = view.annotation?.coordinate else { return }
        var indexOfAnnotation = 0
        for (index , item) in myAnnotations.enumerated() {
            if annotationLocation.latitude == item.coordinate.latitude{
                indexOfAnnotation = index
                break
            }
        }
        let indexPathOfMagorCell = IndexPath(row: indexOfAnnotation, section: 0)
        placesCollection.scrollToItem(at: indexPathOfMagorCell, at: .centeredHorizontally, animated: true)
    }
}

Working properly

After app switching

Upvotes: 3

Views: 588

Answers (1)

C Mch
C Mch

Reputation: 21

Switching from MKPinAnnotationView to MKAnnotationView in the viewFor annotation func fixed this issue for me:

Old:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if !(annotation is CustomMapItemAnnotation) { return nil }
        
        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id")
        
        if (annotation is CustomMapItemAnnotation) {
            annotationView.canShowCallout = true
            if let placeAnnotation = annotation as? CustomMapItemAnnotation {
                if let type = placeAnnotation.type {
                    print("\(type)-color")
                    annotationView.image = UIImage(named: "\(type)-color")
                }
            }
        }
        return annotationView
    }

New:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if !(annotation is CustomMapItemAnnotation) { return nil }
        
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "id")
        
        if (annotation is CustomMapItemAnnotation) {
            annotationView.canShowCallout = true
            if let placeAnnotation = annotation as? CustomMapItemAnnotation {
                if let type = placeAnnotation.type {
                    print("\(type)-color")
                    annotationView.image = UIImage(named: "\(type)-color")
                }
            }
        }
        return annotationView
    }

Upvotes: 1

Related Questions