Eric Smith
Eric Smith

Reputation: 1376

Custom MKAnnotationView with multiple images

enter image description here

Above is an example of something I am trying to do. I have two images, "marker.png" and "userImage.png". I can get each to display individually as an annotation, but I want to overlay the userImage on the marker background as one annotation. It seems the picture above does something like this with the white marker and a picture on top. I see MKAnnotationView takes a UIImage as a property but no UIImageView. How can I accomplish what I am trying to do?

Currently I am able to just show one image

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if !(annotation is MKPointAnnotation) {
        return nil
    }

    let reuseId = "CustomMarker"

    var markerView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if markerView == nil {
        markerView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        markerView.image = UIImage(named: "marker.png")
    }
    else {
        markerView!.annotation = annotation
    }

    return markerView
}

Upvotes: 0

Views: 1372

Answers (1)

matt
matt

Reputation: 534885

Implement your own MKAnnotationView subclass. Now you've got a view that's all yours! It can have whatever subviews you want, or (this is what I would do) you can just implement drawRect: and draw into it; drawing a composite or arrangement of images in drawRect: is easy.

Or, you could just keep doing what you're doing but composite the images in code and use the resulting combined image as the image property. That's easy too.

Upvotes: 2

Related Questions