3

マップビューにカスタム画像で注釈を表示するためのコードをいくつか作成しました。私の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;
}

}

問題は、マップ上のカスタムアノテーションを長押しすると、奇妙な動作になります。画像が変化し、デフォルトの赤いピンが表示されます。

なぜこの振る舞い?そして、どうすればそれを回避できますか?

4

2 に答える 2

3

MKAnnotationView注釈ビューにカスタム画像を使用する場合は、の代わりにジェネリックを作成しますMKPinAnnotationView

MKPinAnnotationViewピンであるデフォルトの画像を表示するのが本当に好きです。

ロジックを少し再配置してFONTANELLA、の場合はを作成MKPinAnnotationViewし、残りの場合はを作成しMKAnnotationViewます。

また、すべての場合にアノテーションビューの再利用を実際に実装する必要があります(elseデキューが何も返さない場合は何も行われないため、最後の部分は意味がありませんreturn nil;。代わりに行うことができます)。

于 2011-11-11T18:33:35.530 に答える
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;

}
于 2011-11-18T09:10:12.593 に答える