1

サブクラス化された mkannotationview に奇妙な問題が見つかりました。

最初の 5 つのマーカーを追加すると、それらはすべて完全に機能します。mkannotationview サブクラスでは、5 回表示されるメッセージを NSLog します。ただし、すべてのマーカーを削除して再描画すると、すべて同じ方法を使用して、NSLog が 1 回しか表示されません。

マップが既存の注釈ビューを再利用しているようなものですか? 毎回新しいものを強制的に使用する方法はありますか?

[コードで更新]

したがって、再利用できない理由 (これが問題である場合とそうでない場合があります) は、ラベル付きの一意のマーカーを作成しているためです。マーカーのラベルには、個々のマーカーへの参照が含まれています (製品 ID のように考えてください)。

だから... ProductPlot.hで

@interface ProductPlot : NSObject <MKAnnotation> {
    NSString *productID;
    float latitude;
    float longitude;
}
@property (nonatomic, copy) NSString *productID;

および ProductPlot.m

@implementation ProductPlot
@synthesize productID;

- (CLLocationCoordinate2D)coordinate {
    CLLocationCoordinate2D coord = {self.latitude, self.longitude};
    return coord;
}

- (NSString *) productID {
    return productID;
}

次に、ProductPlotView.h としてサブクラス化された注釈ビューがあります。

@interface ProductPlotView : MKAnnotationView {
ProductPlot *product;
}

および ProductPlotView.m で

@implementation ProductPlotView

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {

        product = (ProductPlot *)annotation;
        UILabel *plate2 = [[[UILabel alloc] init] autorelease];
        plate2.text = product.productID;
        plate2.frame = CGRectMake(35, 4, 100, 30);
        plate2.backgroundColor = [UIColor clearColor]; //]clearColor];
        [plate2 setFont: [UIFont fontWithName: @"myFont" size: plate2.font.pointSize]];

        [self addSubview:plate2];
    }
    self.frame = CGRectMake(0,0,133,40);

    return self;
}

したがって、私のコードでは、次を使用してポイントをプロットします

- (void)plotPoint: (int) y latitude: (double) lat longitude: (double) lng productID: (NSString *) pID {

    ProductPlot *newAnnotation = [[ProductPlot alloc] init];
    newAnnotation.latitude = lat;
    newAnnotation.longitude = lng;
    newAnnotation.productID = pID;
    [mapView addAnnotation:newAnnotation];

    [newAnnotation release];

}

注釈を処理するコードもあります。

- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {



    ProductPlotView *eventView = (ProductPlotView *)[lmapView
                                    dequeueReusableAnnotationViewWithIdentifier:@"eventview"];
    if(eventView == nil) {
        eventView = [[[VehicleViewInfo alloc] initWithAnnotation:annotation
                                                     reuseIdentifier:@"eventview"]
                     autorelease];
    }

    eventView.annotation = annotation;
    return eventView;
}

したがって...上記はproductIDを取得し、マップマーカーであるラベルに配置します。これは FIRST プロット インスタンスで完全に機能するようですが、( を使用して[mapView removeAnnotations:mapView.annotations];) マーカーを削除してからこの関数を再度呼び出すと、ポイントは正常に描画されますが、製品 ID は正しくありません。それらをランダムに描画するように見えます。

注: 上記のコードはいくつかの部分が削除されているため、タイプミスがある可能性があります

情報をありがとう。

4

2 に答える 2

0

注釈ビューでサブビューをチェックする同様の問題を解決しました。サブビューがない場合は、新しいラベルを作成してサブビューとして追加します。サブビューがある場合は、最初のものを取得して label.text プロパティを編集します。このように、サブビューは常にゼロまたは 1 つだけです。

于 2012-09-05T13:59:06.457 に答える
0

自分の質問に答えるために、コードMKAnnotationView* annotationView = nil;の先頭に追加しました。- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation

今では完璧に機能しているようです。他の人がこれがこれを行う正しい方法だと思うかどうかを知りたいですか?

于 2010-08-30T18:14:11.407 に答える