在iOS中的CommentationView中自定义图钉图像

乔迪·加米斯(JordiGámez)

我正在尝试从Swift 1.2更改为Swift 2.0,而我在更改的最后。目前,我正在MapViewController中进行更改,并且没有任何错误或警告,但是我的图钉(annotationView)的自定义图片未分配给图钉,而是显示了默认图片(红点)。

这是我的代码,希望您能提供一些帮助,因为我认为一切都很好,但仍然无法正常工作:

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
}

提前致谢,

问候。

安德烈·戈尔捷夫(Andrey Gordeev)

接受的答案无效,因为它尚未annotationViewelse块中初始化

这是一个更好的解决方案。如果可能,它将使注释视图出队,否则将创建新视图:

斯威夫特3,4

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 2.2

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

    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
        annotationView = av
    }

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

    return annotationView
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Mapview Swift iOS中设置自定义图钉图像?

无法在 MapView 中设置自定义图钉图像,Swift 3

不会添加自定义图钉图像

MapKit在iOS9上未显示自定义注释图钉图像

iOS 7中UINavigation后退按钮的自定义图像

自定义地图图钉图像Objective-C

拼贴中的自定义图像角度

MapKit中具有字母数字表示形式的自定义图钉

在Xamarin Forms中的自定义地图图钉上显示网址

Android:Google Map API 2中带有按钮的自定义图钉标记

在iOS导航控制器中的代码中自定义图像大小

如何在iOS中的自定义tableviewcell中的图像上添加文本标签

如何以编程方式从 iOS 中的自定义相册中删除图像?

SceneKit中的iOS自定义几何

iOS 7中的自定义字体

自定义iOS框架中的资源

iOS中的自定义进度栏

Xamarin iOS中的自定义振动

iOS中的自定义推送通知

iOS应用中的自定义字体

iOS Dev:在 MapBox 中为用户注释使用自定义图像

如何在iOS7中为后退按钮创建自定义蒙版图像

iOS 11中的导航栏自定义图像视图问题

所显示的UIActivityViewController中的iOS 13,自定义图像,标题和字幕

iOS 7.1中的UIProgressView自定义跟踪和进度图像

如何在新的Google+ iOS SDK中自定义GIDSignInButton登录图像?

如何使用自定义键盘扩展iOS 8在textDocumentProxy中设置图像?

IOS / Objective-C:在自定义单元格中制作圆形图像的代码

如何从iOS中的自定义视频播放器屏幕捕获静止图像?