2011-11-11 58 views
3

我已經編寫了一些代碼,用於在mapview中顯示帶有自定義圖像的註釋。ios-mapkit,自定義圖像註釋的奇怪行爲

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation { 
if ([annotation isKindOfClass:[Station class]]) { 
    Station *current = (Station *)annotation; 
    MKPinAnnotationView *customPinview = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil]; 
    if([[current type] compare:FONTANELLA]==NSOrderedSame) 
     customPinview.pinColor = MKPinAnnotationColorPurple; 
    else{ 
     int test=current.bici; 
     if(test==0) 
      customPinview.image = [UIImage imageNamed:@"bicimir.png"]; 
     else if(test<4) 
      customPinview.image = [UIImage imageNamed:@"bicimi.png"]; 
     else if(test>=4) 
      customPinview.image = [UIImage imageNamed:@"bicimig.png"]; 
    } 
    customPinview.animatesDrop = NO; 
    customPinview.canShowCallout = YES; 
    return customPinview; 
} 
else{ 
    NSString *[email protected]"MyLocation"; 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    return annotationView; 
} 

}

的問題是一個奇怪的現象,當我長點擊地圖上的自定義註釋: 的 當他們放在地圖我MapView的委託實現此方法自定義註釋圖像變化並顯示默認的紅色針腳。

爲什麼會這樣?我怎樣才能避免它?

回答

3

如果要爲註釋視圖使用自定義圖像,請創建一個通用的MKAnnotationView而不是MKPinAnnotationView

MKPinAnnotationView真的很喜歡顯示它的默認圖像,這是一個pin。

重新排列邏輯有點,所以對於FONTANELLA,它創建一個MKPinAnnotationView但其餘的MKAnnotationView

此外,你應該真的實現所有情況下的註解視圖重用(最後else部分沒有意義,因爲如果出列沒有返回任何內容,沒有做任何事情 - 你可以改爲return nil;) 。

+0

感謝您的答覆!這非常有用 – Matteo

0

.h文件中

@interface AddressAnnotation : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *mPinColor; 

} 

@property (nonatomic, retain) NSString *mPinColor; 

@end 

裏面.m文件

@implementation AddressAnnotation 

@synthesize coordinate mPinColor; 


- (NSString *)pincolor{ 
    return mPinColor; 
} 

- (void) setpincolor:(NSString*) String1{ 
    mPinColor = String1; 
} 


-(id)initWithCoordinate:(CLLocationCoordinate2D) c{ 
    coordinate=c; 
    NSLog(@"%f,%f",c.latitude,c.longitude); 
    return self; 
} 
@end 

裏面的.m類文件

- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(AddressAnnotation *) annotation{ 

    UIImage *anImage=[[UIImage alloc] init]; 

MKAnnotationView *annView=(MKAnnotationView*)[mapView1 dequeueReusableAnnotationViewWithIdentifier:@"annotation"]; 

    if(annView==nil) 
    { 
     annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"] autorelease]; 

    } 

    if([annotation.mPinColor isEqualToString:@"green"]) 
    { 

     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google pin green.png" ofType:nil]]; 
    } 
    else if([annotation.mPinColor isEqualToString:@"red"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google pin red.png" ofType:nil]]; 
    } 
    else if([annotation.mPinColor isEqualToString:@"blue"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google pin blue.png" ofType:nil]]; 
    } 

    annView.image = anImage; 

    return annView; 

}