3

我尝试快速为我的 MKPointAnnotation 创建一个自定义“徽章”,但它失败了,因为 MKPointAnnotation 没有像图像这样的任何属性

 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)

有人想出一个快速的解决方案吗?

4

1 回答 1

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

}
于 2015-09-04T21:54:27.013 回答