iOS中annotationView中的自定义pin图像

iOS中annotationView中的自定义pin图像,ios,swift,mkmapview,mkannotationview,Ios,Swift,Mkmapview,Mkannotationview,我正在尝试从Swift 1.2更改为Swift 2.0,我已经完成了更改。目前我正在MapViewController中进行更改,没有任何错误或警告,但我的pin的自定义图像(annotationView)没有指定给pin,它显示的是默认的pin(红点) 这是我的代码,我希望你能帮我一些提示,因为我认为一切都很好,但它仍然不起作用: func parseJsonData(data: NSData) -> [Farmacia] { let farmacias = [Farmacia

我正在尝试从Swift 1.2更改为Swift 2.0,我已经完成了更改。目前我正在MapViewController中进行更改,没有任何错误或警告,但我的pin的自定义图像(annotationView)没有指定给pin,它显示的是默认的pin(红点)

这是我的代码,我希望你能帮我一些提示,因为我认为一切都很好,但它仍然不起作用:

func parseJsonData(data: NSData) -> [Farmacia] {

    let farmacias = [Farmacia]()

    do
    {
        let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

        // Parse JSON data
        let jsonProductos = jsonResult?["farmacias"] as! [AnyObject]

        for jsonProducto in jsonProductos {

            let farmacia = Farmacia()
            farmacia.id = jsonProducto["id"] as! String
            farmacia.nombre = jsonProducto["nombre"] as! String
            farmacia.location = jsonProducto["location"] as! String

            let geoCoder = CLGeocoder()
            geoCoder.geocodeAddressString(farmacia.location, completionHandler: { placemarks, error in

                if error != nil {
                    print(error)
                    return
                }

                if placemarks != nil && placemarks!.count > 0 {

                    let placemark = placemarks?[0]

                    // Add Annotation
                    let annotation = MKPointAnnotation()
                    annotation.title = farmacia.nombre
                    annotation.subtitle = farmacia.id
                    annotation.coordinate = placemark!.location!.coordinate

                    self.mapView.addAnnotation(annotation)
                }

            })
        }
    }
    catch let parseError {
        print(parseError)
    }

    return farmacias
}

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

    let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    // Reuse the annotation if possible
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

    if annotationView == nil
    {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.canShowCallout = true
    }

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

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
    annotationView!.rightCalloutAccessoryView = detailButton

    print(annotationView!.image)

    return annotationView
}
提前感谢,

问候。

以下是答案:

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

    let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)

    if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "custom_pin.png")
        annotationView.rightCalloutAccessoryView = detailButton
    }
    else {
        annotationView.annotation = annotation
    }

    return annotationView
}

关于

接受的答案不起作用,因为它在
else
块中未初始化
annotationView

这里有一个更好的解决方案。如果可能,它将退出注释视图,如果没有,则创建一个新的注释视图:

// https://stackoverflow.com/a/38159048/1321917
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    // Don't want to show a custom image if the annotation is the user's location.
    guard !(annotation is MKUserLocation) else {
        return nil
    }

    // Better to make this class property
    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }

    if let annotationView = annotationView {
        // Configure your annotation view here
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "yourImage")
    }

    return annotationView
}
SWIFT 3,4

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

    if annotation.isKind(of: MKUserLocation.self) {
        return nil
    }

    let annotationIdentifier = "AnnotationIdentifier"

    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
    if (pinView == nil) {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    }

    pinView?.canShowCallout = false

    return pinView
}

感谢分享您的解决方案。它帮助我否决了投票。1) “annotationView”实例是在if-let范围中创建的,而在else范围中不存在。2) 如果您成功地将annotationview出列,那么您应该使用它,而不是创建新实例。新实例应该在else作用域中创建,可重用实例不能在该作用域中退出队列。我认为作为mkannotation的子代,需要MKPinAnnotation。这对我的帮助比公认的答案大得多,非常感谢@Andrey Gordevow!!!这真的救了我!!!我遇到了一些严重的性能问题,但是看到这个关于如何正确使用标识符的示例确实很有帮助。我认为我一开始也在正确地使用它。这应该是公认的答案!伟大的谢谢。如果您在声明“annotationView”时退出队列,实际上可以简化此过程。看看我的答案。