无法显示自定义图片图片

时间:2017-05-04 16:32:42

标签: ios swift annotations mkmapview mkannotation

我试图让自定义图像图像显示在我的mapView中,并且它呈现常规红色图钉而不是我的自定义图像。我做错了什么?

ViewFor Annotation:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }
        let reuseID = "pin"

        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID) as? MKPinAnnotationView
        if(pinView == nil) {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = false
            pinView?.image = UIImage(named: "CustomPinImage")
            pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure) as UIButton
            let smallSquare = CGSize(width: 40, height: 40)
            let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
            button.setBackgroundImage(UIImage(named: "Car2"), for: UIControlState())
            pinView?.leftCalloutAccessoryView = button

        }
        else
        {
            pinView?.annotation = annotation
        }

        return pinView

    }

我如何显示Pins:

let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192)
        let Litzman = MKPointAnnotation()
        Litzman.coordinate = LitzmanLocation
        Litzman.title = "Litzman Bar"
        Litzman.subtitle = "נמל תל אביב 18,תל אביב"
        mapView.addAnnotation(Litzman)

如果有人能帮助我解决这个问题,那就太棒了!

谢谢:)

1 个答案:

答案 0 :(得分:0)

试试这段代码:

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

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

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pinView == nil {
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)}
        pinView!.canShowCallout = true
        pinView!.image = UIImage(named: "CustomPinImage")
        pinView?.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure) as UIButton
        let smallSquare = CGSize(width: 40, height: 40)
        let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
        button.setBackgroundImage(UIImage(named: "Car2"), for: UIControlState())
        pinView?.leftCalloutAccessoryView = button

        return pinView

    }