2012-08-06 109 views
3

我想在地圖上添加自定義圖像,而不是常規圖釘。但它仍然是一個紅色的針......我錯過了什麼?MKAnnotationView自定義圖像不顯示

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]  initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; 
    annView.animatesDrop = TRUE; 
    annView.image = [UIImage imageNamed:@"CustomPin.png"]; 
    return annView; 
} 
+0

見http://stackoverflow.com/questions/10501341/ mkpinannotationview-custom-image-is-replace-by-pin-with-animating-drop – Anna 2012-08-06 14:16:53

+0

你的代碼工作,但缺少動畫Drop。我加了pinView.animatesDrop = YES;但有一個錯誤 - 屬性animatesDrop沒有在MKAnnotationView上找到。所以,修復它:http://stackoverflow.com/a/2087253/1341180 – Luda 2012-08-06 15:21:44

+0

如果你會把這個鏈接作爲答案,我會檢查它是正確的 – Luda 2012-08-06 15:22:39

回答

5

MKMapView: Instead of Annotation Pin, a custom view

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    { 
     static NSString *defaultPinID = @"com.invasivecode.pin"; 
     pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinView == nil) 
      pinView = [[MKAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

     //pinView.pinColor = MKPinAnnotationColorGreen; 
     pinView.canShowCallout = YES; 
     //pinView.animatesDrop = YES; 
     pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch 
    } 
    else { 
     [mapView.userLocation setTitle:@"I am here"]; 
    } 
    return pinView; 
} 
3

嗨只是刪除您一行代碼... annView.animatesDrop = TRUE;

次還代碼 -

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]  initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; 
    annView.image = [UIImage imageNamed:@"CustomPin.png"]; 
    return annView; 
} 
相關問題