不会显示自定义引脚图像

问题描述:

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

ViewFor译注:

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 

    } 

如何我显示引脚:

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

这将是巨大的,如果有人可以帮助我整理了这一点!

谢谢:)

试试这个代码:

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 

    } 
+0

谢谢!有用 ! –