1

my problem is that annotationView.image = X doesn't work, I just can't see the special pin, just purple(pinTintColor) if I remove it - it becomes red..

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.delegate = self

    for mall in malls {
        let coords = CLLocation(latitude: mall.latitude, longitude: mall.longitude)
        let annotation = MyAnnotation.init(coordinate: coords.coordinate, mall: mall, title: mall.name, subtitle: mall.adress)

        self.mapView.addAnnotation(annotation)
    }
}

and also i can't add image as accessoryview. What's the problem?

 extension commonMapViewController: MKMapViewDelegate {

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

    let annotationIdentifier = "restAnnotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = rightImage
        annotationView?.canShowCallout = true

    }
    annotationView?.pinTintColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)

    annotationView?.image = UIImage(named: "malls.png")

    return annotationView
}

Thanks for your help and excuse me for my stupidness..

Rob
  • 415,655
  • 72
  • 787
  • 1,044
dmarinkin
  • 43
  • 8
  • The problem is that you're using `MKPinAnnotationView`. In old iOS versions, you could set the `image`, but not in recent versions. Use `MKAnnotationView`. – Rob Dec 17 '16 at 03:47
  • Also, if you successful dequeue a prior annotation view, remember to set its `annotation` in an `else` clause to your `if` statement. – Rob Dec 17 '16 at 03:49

1 Answers1

2

I'm not sure what you want to achieve but I see 2 possibilities:

1. If you want a purple pin with a right-image in the callout

MKPinAnnotationView.image is set to that of a pin, you can't change it. The code below will make a purple pin whose popup have a right-image showing Apple's HQ.

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

    let annotationIdentifier = "restAnnotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MKPinAnnotationView

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

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        imageView.image = #imageLiteral(resourceName: "rightImage")
        imageView.contentMode = .scaleAspectFit

        annotationView?.rightCalloutAccessoryView = imageView
    }

    annotationView?.annotation = annotation
    annotationView?.pinTintColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
    return annotationView
}

Purple pin

2. If you want to have an image on the map

Use MKAnnotationView instead. You can set its image property to whatever you want. You can also combine with the code above to add a right-image

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

        let annotationIdentifier = "restAnnotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView?.canShowCallout = true
        }

        annotationView?.annotation = annotation
        annotationView?.image = #imageLiteral(resourceName: "rightImageSmall")
        return annotationView
    }

Annotation with custom image

Code Different
  • 90,614
  • 16
  • 144
  • 163
  • @Rob 1st comment: agreed. I missed that. Edited my answer. 2nd comment: I thought the OP may have wanted a purple pin so the first snippet use `MKPinAnnotationView`. As stated in the answer, the second snipper can be combined with the first snippet to make an annotation with custom image and right-image. – Code Different Dec 17 '16 at 04:00