9

以下の何が間違っているかについての提案をいただければ幸いです。

次のコードを使用して、注釈にカスタム イメージを追加しています。

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isMemberOfClass:[MKUserLocation class]]) 
    { 
        return nil; 
    } 


    if ([annotation isMemberOfClass:[SpectatorPin class]]) 
    { 
        SpectatorPin *sp = (SpectatorPin *)annotation;
        MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"];
        if (view == nil) {
            view = [[[MKPinAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease];
        }
        view.image = [UIImage imageNamed:@"mylocation20x20.png"]; 
        view.canShowCallout = YES;
        view.annotation=annotation;
        return view;
    }

    //Should not get here
    return nil;

}

最初はちゃんと画像が表示されています。

マップ タイプ (標準、衛星、ハイブリッド) を変更するセグメント コントロールがあります。標準がデフォルトです。衛星を選択するとすぐに、画像がピンに変わります。mapView:viewforAnnotation メソッドは再度呼び出されません。

よろしく、

ジム

4

3 に答える 3

24

カスタム イメージの場合、MKPinAnnotationView の代わりに MKAnnotationView を使用できます。

MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"];
        if (view == nil) {
            view = [[[MKAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease];
        }
于 2011-06-24T03:50:32.833 に答える
1

これを見つけました: iPhoneコアの場所:マップタイプが変更されるとカスタムピン画像が消えます

これにリンクされている: カスタム MKMapView 注釈画像がタッチすると消えるのはなぜですか?

于 2011-03-30T00:46:20.843 に答える