【问题标题】:ios-mapkit, strange behavior with custom image annotationsios-mapkit,自定义图像注释的奇怪行为
【发布时间】:2011-12-27 05:58:39
【问题描述】:

我已经编写了一些代码,用于在地图视图中显示带有自定义图像的注释。 我的 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;
}

}

当我长按地图上的自定义注释时,问题是一种奇怪的行为: 图像发生变化并显示默认的红色图钉。

为什么会有这种行为?我该如何避免呢?

【问题讨论】:

    标签: ios mapkit mkpinannotationview


    【解决方案1】:

    当您想为注释视图使用自定义图像时,请创建通用 MKAnnotationView 而不是 MKPinAnnotationView

    MKPinAnnotationView 真的很喜欢显示它的默认图像,这是一个图钉。

    稍微重新排列逻辑,以便为FONTANELLA 创建一个MKPinAnnotationView,而为其余的一个MKAnnotationView

    此外,您应该真正实现所有情况下的注释视图重用(最后else 部分没有意义,因为如果出队没有返回任何内容,则什么都不做——您可以只做@987654328 @ 代替)。

    【讨论】:

    • 感谢您的回复!这个很有用
    【解决方案2】:

    在.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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      相关资源
      最近更新 更多