2011-11-11 60 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; 

}