MKAnnotationView不显示图像

时间:2016-09-30 14:26:41

标签: swift mkmapview

这是一个小问题..

我已经完成了一些代码,这些代码应该更改在mapView中删除的引脚图像。 “taxi.png”的网址在图片视图上工作,这不是当前的问题。

我也尝试过:

pinView?.image = UIImage(named: "taxi.png")

代码:

 if pinView == nil{
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true
        var imageTaxi = UIImage(named: "taxi.png")!
        imageTaxi = imageTaxi.withAlignmentRectInsets(UIEdgeInsetsMake(0, 0, imageTaxi.size.height/2, 0))
        pinView?.image = imageTaxi
        }

        let button = UIButton(type: .detailDisclosure)
        pinView?.rightCalloutAccessoryView = button
        return pinView

有人看到这里应该修复什么来查看图像吗?

修改 我现在有这个,我仍然有标准的红色针点..

 let annotation = CustomPin()
 annotation.coordinate = location
 annotation.title = "Boek Taxi"
 annotation.subtitle = ""
 let image = UIImage(named: "taxi.png")

 annotation.Image = image
 self.mapView.addAnnotation(annotation)
 let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)

    if pinView == nil{
        pinView = MKPinAnnotationView(annotation: CustomPin() as MKAnnotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true

    } else {
        pinView?.annotation = CustomPin() as MKAnnotation
    }

    let cpa = annotation as! CustomPin

    pinView?.image = cpa.Image

    let button = UIButton(type: .detailDisclosure)
    pinView?.rightCalloutAccessoryView = button
    return pinView
    }

我看过的任何东西都在看?

1 个答案:

答案 0 :(得分:-1)

试试这个

为自定义注释创建一个类 示例:

class CustomPin: MKPointAnnotation {

   let Image = UIImage

 }

然后在您的注释视图的功能中执行此操作

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
         let reuseId = "image"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if pinView == nil {

            pinView = MKAnnotationView(annotation: customPin as MKAnnotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true

        } else {

            pinView!.annotation = customPin as MKAnnotation
        }

        let cpa = annotation as! CustomPin
        pinView!.image = cpa.Image

        let button = UIButton(type: UIButtonType.detailDisclosure)

        pinView!.rightCalloutAccessoryView = button


        return anView


        }

最后在viewDidLoad()或您想要的任何内容中创建注释时添加自定义图像:

                                let Annotation = customPin()
                                Annotation.coordinate = //cooridnate
                                Annotation.title = //title
                                Annotation.subtitle = //subtitle

                                let image = UIImage(named: "taxi.png")
                                Annotation.Image = image

希望这有帮助