ios-mapkit,自定义图像注释的奇怪行为

时间:2011-11-11 18:21:47

标签: ios mapkit mkpinannotationview

我已经编写了一些代码,用于在mapview中显示带有自定义图像的注释。 我的mapview委托实现此方法,以便在将它们放入地图时自定义注释:

- (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 *identifier=@"MyLocation";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    return annotationView;
}

}

当我长时间点击地图上的自定义注释时,问题是一种奇怪的行为: 显示图像更改和默认红色引脚。

为什么会这样?我怎么能避免它?

2 个答案:

答案 0 :(得分:3)

如果要将自定义图像用于注释视图,请创建通用MKAnnotationView而不是MKPinAnnotationView

MKPinAnnotationView非常喜欢显示默认图像,即图钉。

稍微重新排列逻辑,以便FONTANELLA创建MKPinAnnotationView但其余为MKAnnotationView

另外,你应该真正实现所有情况下的注释视图重用(并且最后的else部分没有意义,因为如果出列没有返回任何内容,则没有任何事情 - 你可以改为return nil;

答案 1 :(得分:0)

在.h文件中

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

}

@property (nonatomic, retain) NSString *mPinColor;

@end
<。>文件中的

@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;

}
相关问题