具有多个图像的自定义MKAnnotationView

时间:2014-12-05 00:52:13

标签: ios swift mkmapview mapkit

enter image description here

以上是我想要做的事情的一个例子。我有两张图片," marker.png"和" userImage.png"。我可以将每个单独显示为注释,但我想将标记背景上的userImage叠加为一个注释。看起来上面的图片用白色标记和顶部的图片做了类似的事情。我看到MKAnnotationView将UIImage作为属性但没有UIImageView。我怎样才能完成我想做的事情?

目前我只能显示一张图片

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
}

1 个答案:

答案 0 :(得分:2)

实现自己的MKAnnotationView子类。现在,您已经了解了所有人的观点!它可以包含你想要的任何子视图,或者(这就是我要做的)你可以实现drawRect:并绘制它;在drawRect:中绘制合成或排列的图像很容易。

或者,你可以继续做你正在做的事情但是在代码中合成图像并使用得到的组合图像作为image属性。这也很容易。

相关问题