2011-03-01 258 views
9

任何有關以下錯誤的建議將不勝感激。iOS MapKit更改mapview maptype導致註釋圖像更改爲pin?

我正在使用以下代碼將自定義圖像添加到註釋中。

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
      viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isMemberOfClass:[MKUserLocation class]]) 
    { 
     return nil; 
    } 


    if ([annotation isMemberOfClass:[SpectatorPin class]]) 
    { 
     SpectatorPin *sp = (SpectatorPin *)annotation; 
     MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"]; 
     if (view == nil) { 
      view = [[[MKPinAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease]; 
     } 
     view.image = [UIImage imageNamed:@"mylocation20x20.png"]; 
     view.canShowCallout = YES; 
     view.annotation=annotation; 
     return view; 
    } 

    //Should not get here 
    return nil; 

} 

圖像在初始顯示正確。

我有一個段控制,它改變了地圖類型(標準,衛星,混合)。標準是默認值。只要我選擇衛星,圖像立即變成一個針。 mapView:viewforAnnotation方法不會再被調用。

問候,

吉姆

回答

24

對於自定義圖像,你可以使用,而不是MKPinAnnotationView MKAnnotationView。

MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"]; 
     if (view == nil) { 
      view = [[[MKAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease]; 
     } 
+1

它幫助我:) – iSwaroop 2014-03-11 09:33:28

1

試試這個:

MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"]; 
     if (view == nil) { 
      view = [[[MKAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease]; 
     }