Swift - MKPinAnnotation图像问题

时间:2014-12-09 00:02:56

标签: ios swift mkannotation mkpinannotationview

我试图将我的引脚注释图像更改为引脚以外的其他内容。

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

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Green
            pinView!.image = UIImage(named: "icon1.png")

            // Add image to left callout
            var mugIconView = UIImageView(image: UIImage(named: "test.png"))
            pinView!.leftCalloutAccessoryView = mugIconView

            // Add detail button to right callout
            var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
            pinView!.rightCalloutAccessoryView = calloutButton
        }
        else {
            pinView!.annotation = annotation
        }

        return pinView

除了更改图像图像外,一切正常。我有一个自定义点注释子类,这是

我一直在寻找几个小时,但我发现的每一个问题都没有帮助。我很感激任何帮助......谢谢。

更新答案

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

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin")

    if pinView == nil {
        pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        pinView!.canShowCallout = true
        pinView!.image = UIImage(named: "test.png")

        // Add image to left callout
        var mugIconView = UIImageView(image: UIImage(named: "test.png"))
        pinView!.leftCalloutAccessoryView = mugIconView

        // Add detail button to right callout
        var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
        pinView!.rightCalloutAccessoryView = calloutButton
    }
    else {
        pinView!.annotation = annotation
    }

    return pinView
}

1 个答案:

答案 0 :(得分:3)

您需要MKAnnotationView,而不是MKPinAnnotationView。引脚注释无法自定义其图像。

请参阅this SO answer for additional details