1

我刚刚发现我的子类 mkannotationview 存在一个奇怪的问题。

当我添加前 5 个标记时,它们都可以正常工作。在 mkannotationview 子类中,我 NSLog 一条我看到 5 次的消息。但是,当我删除所有标记并重新绘制它们时 - 使用所有相同的方法,我只看到一次 NSLog。

就像地图正在重用现有的注释视图?有没有办法强制它每次都使用新的?

[更新代码]

所以我不能重用的原因(这可能是也可能不是问题)是我正在创建带有标签的独特标记。标记上的标签包含对单个标记的引用(将其视为产品 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

为了回答我自己的问题,我在代码MKAnnotationView* annotationView = nil;的顶部添加了内容- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation

现在它似乎工作得很好。我仍然很想看看其他人是否认为这是正确的方法?

于 2010-08-30T18:14:11.407 回答
0

我解决了在注释视图上检查子视图的类似问题。如果它没有子视图,我创建一个新标签并将其添加为子视图,如果它有子视图,我将采用第一个并编辑 label.text 属性。这样子视图总是为零或只有一个。

于 2012-09-05T13:59:06.457 回答