2016-04-27 90 views
3

我试图将MKMapView上的用户默认位置注释从蓝色更改为名为geo的自定义图像。当我设置断点时,它会将它设置为地理位置,但这两个点(用户默认和Passenger点都是默认的红色精确定位注释)我是否设置了错误,或者是否存在某些图像规定?更改MKMapView用户位置注释

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

     if annotation.isKindOfClass(PassengerLocation) == false { 

      //User location 
      let userIdentifier = "UserLocation" 
      var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(userIdentifier) 

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

      // THIS IS NOT WORKING, DEFAULT RED PIN POINT STILL SHOWING 
      annotationView!.image = UIImage(named: "geo") 
      return annotationView 


     } 

     let identifier = "PassengerLocation" 

     if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) { 
      annotationView.annotation = annotation 

      return annotationView 
     } else { 
      let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier) 
      annotationView.enabled = true 
      annotationView.canShowCallout = true 

      let btn = UIButton(type: .DetailDisclosure) 
      annotationView.rightCalloutAccessoryView = btn 

      return annotationView 
     } 

    } 

回答

0

如果要定制图像,请使用常规MKAnnotationView。用一个别针,你所能做的就是改变颜色。

3

这工作:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     if annotation.isEqual(mapView.userLocation) { 
     let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation") 
     annotationView.image = UIImage(named: "geo") 
     return annotationView 
    } 
} 
+0

你觉得如何? – 123FLO321

0

它的工作对我来说

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

    // want to show a custom image if the annotation is the user's location. 
    guard !annotation.isKindOfClass(MKUserLocation) else { 
     let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation") 
     annotationView.image = UIImage(named: "icon_coordinates_self") 
     return annotationView 
     //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: "Annotation_map") 
    } 

    return annotationView 
}