Swift MKPointAnnotation自定义图像

时间:2014-09-02 13:34:58

标签: ios swift mapkit mkpointannotation

我尝试在swift中为我的MKPointAnnotation创建一个自定义“badget”,但由于MKPointAnnotation没有像image这样的属性而失败了

 var information = MKPointAnnotation()
    information.coordinate = location 
    information.title = "Test Title!"
    information.subtitle = "Subtitle"
    information.image = UIImage(named: "dot.png") //this is the line whats wrong
    Map.addAnnotation(information)

有人为此找到了一个快捷的解决方案吗?

1 个答案:

答案 0 :(得分:9)

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if !(annotation is MKPointAnnotation) {
        return nil
    }

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("demo")
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "demo")
        annotationView!.canShowCallout = true
    }
    else {
        annotationView!.annotation = annotation
    }

    annotationView!.image = UIImage(named: "image")

    return annotationView

}