2011-06-09 73 views

回答

55

viewForAnnotation:MKMapViewDelegate方法也許你會爲這樣的代碼。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 

    if (annotation == mapView.userLocation) return nil; 
    ... 

回到如果註釋用戶位置的MapView顯示藍點&圈動畫。爲了顯示我們的用戶位置的自定義註釋,只需刪除行return nil;並在那裏進行自定義。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 

    static NSString* AnnotationIdentifier = @"Annotation"; 
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 

    if (!pinView) { 

     MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
     if (annotation == mapView.userLocation){ 
      customPinView.image = [UIImage imageNamed:@"myCarImage.png"]; 
     } 
     else{ 
      customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"]; 
     } 
     customPinView.animatesDrop = NO; 
     customPinView.canShowCallout = YES; 
     return customPinView; 

    } else { 

     pinView.annotation = annotation; 
    } 

    return pinView; 
} 
+0

完美的作品:)謝謝! – LouwHopley 2011-06-09 15:24:36

+1

只是在旁註 - 是否有一種簡單的方法來維護自定義圖像下方的淺藍色「準確性」圓圈? – LouwHopley 2011-06-09 15:31:42

+0

@Nideo,我猜你必須用* userLocation *註解的*座標來創建一個註釋。在* didUpdateToLocation:*方法中,您還需要更新新的註釋。但我不知道這是否會按照您的預期工作。在* if(註解== mapView.userLocation)*的情況下,這可能會失敗,因爲您的新註釋也包含用戶位置的座標。但我不確定。試一試! – EmptyStack 2011-06-10 04:28:24

1

好,這裏是斯威夫特版本:

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

    let identifier = "User" 

     var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

     if annotationView == nil{ 
      annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView.canShowCallout = true 

     } else { 
      annotationView.annotation = annotation 
     } 

    annotationView.image = UIImage(named: "image") 

    return annotationView 

} 
+1

這假定您只有1個註釋。不是最好的解決方案。 – Aggressor 2015-09-22 23:31:34

+1

您需要爲MKAnnotationView更改MKPinAnnotationView,否則它將始終在地圖上繪製一個圖釘。 – adheus 2016-01-28 17:04:24

+0

您必須檢查註釋是否與用戶位置或其他註釋有關。 – Satyam 2017-04-06 03:13:53

4

這裏是雨燕2.0的版本中,你可能有多個引腳。

在這段代碼中,CustomAnnotation只是一個MKAnnotation子類。基本上,如果註釋不是您的自定義類中的一種,那麼它的用戶位置引腳。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? 
{ 
    // This is false if its a user pin 
    if(annotation.isKindOfClass(CustomAnnotation) == false) 
    { 
     let userPin = "userLocation" 
     if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(userPin) 
     { 
      return dequeuedView 
     } else 
     { 
      let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: userPin) 
      mkAnnotationView.image = UIImage(named: C_GPS.ROUTE_WALK_ICON_NAME) 
      let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height/2) 
      mkAnnotationView.centerOffset = offset 

      return mkAnnotationView 
     } 

    } 

    let annotation = annotation as? CustomAnnotation 
    if(annotation == nil) 
    { 
     return nil 
    } 

    let endPointsIdentifier = "endPoint" 
    if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(endPointsIdentifier) 
    { 
     dequeuedView.image = annotation!.uiimage 
     return dequeuedView 
    } else 
    { 
     let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: endPointsIdentifier) 
     mkAnnotationView.image = annotation!.uiimage 
     let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height/2) 
     mkAnnotationView.centerOffset = offset 

     let gesture = UITapGestureRecognizer(target: self, action: "routeTouched:") 
     mkAnnotationView.addGestureRecognizer(gesture) 

     return mkAnnotationView 
    } 
} 
0

是這個改變當前位置的藍點???

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

let identifier = "User" 

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

    if annotationView == nil{ 
     annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
     annotationView.canShowCallout = true 

    } else { 
     annotationView.annotation = annotation 
    } 

    annotationView.image = UIImage(named: "image") 

    return annotationView 

} 
-1

請嘗試這樣的事情。它在Xcode 7和Swift 2中爲我工作。

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 
    } 

    // for other annotation except current location 
    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 
} 
相關問題