prem111
prem111

Reputation: 63

mapkit - displaying custom images

Below is my code displaying annotations, I have a question why custom images are displayed only the second time the view is loaded?

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation is MKPointAnnotation else { return nil }

        let identifier = "Annotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)


        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.isEnabled = true
            annotationView!.canShowCallout = true
            print("1")
        } else {
            annotationView!.annotation = annotation
            print("2")
        }

        let detailAnnotation = annotation as! WaypointsAnnotation
          if (detailAnnotation.type == "Current") {

              annotationView!.image = UIImage(named: "point5")
              //let transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
              //annotationView!.transform = transform

          }
          if (detailAnnotation.type == "Waypoint") {

              annotationView!.image = UIImage(named: "point6")
              //let transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
              //annotationView!.transform = transform

          }

Thanks for reply.

Upvotes: 0

Views: 27

Answers (1)

Jorge
Jorge

Reputation: 1066

Try changing your annotation type from MKPinAnnotationView to MKAnnotationView.

Upvotes: 1

Related Questions