iphone mkannotationview 奇怪的问题 - 可能被重用
我刚刚发现我的子类 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并将其放置在地图标记的标签上。这似乎在第一个绘图实例上完美工作,但是如果我删除标记(使用 [mapView removeAnnotations:mapView.annotations];
),然后再次调用此函数,它会绘制点,但是产品 ID 不正确。它似乎是随机绘制的。
注意:上面的代码有一些 部分内容被删除,因此可能存在拼写错误
感谢您提供任何信息。
I've just found a strange issue with my sub classed mkannotationview.
When I add the first 5 markers, they all work perfectly. In the mkannotationview sub class, I NSLog a message which I see 5 times. However, when I remove ALL the markers and redraw them - using all the same methods, I see the NSLog only once.
It's like the map is reusing existing annotationviews? Is there a way to force it to use new ones each time?
[UPDATE with code]
So the reason I cannot reuse (and this may or may not be the problem) is that I am creating unique markers with a label. The label on the marker contains a reference to the individual marker (consider it like a product ID)
So... in ProductPlot.h
@interface ProductPlot : NSObject <MKAnnotation> {
NSString *productID;
float latitude;
float longitude;
}
@property (nonatomic, copy) NSString *productID;
and ProductPlot.m
@implementation ProductPlot
@synthesize productID;
- (CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D coord = {self.latitude, self.longitude};
return coord;
}
- (NSString *) productID {
return productID;
}
then I have the annotation view sub classed as ProductPlotView.h
@interface ProductPlotView : MKAnnotationView {
ProductPlot *product;
}
and in 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;
}
So then in my code, I plot the points using
- (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];
}
I also have the code to handle the annotations.
- (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;
}
So... the above will take the productID and place it on a label which is the map marker. This seems to work perfectly on the FIRST plot instance, but if I remove the markers (using [mapView removeAnnotations:mapView.annotations];
) then call this function again, it draws the points OK, but the product IDs are not correct. It appears to draw them at random.
NOTE: the code above has had a few
parts removed, so there may be typos
Thanks for any info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了回答我自己的问题,我在
- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id) 的顶部添加了
代码。MKAnnotationView* commentView = nil;
注释现在看来一切正常。我仍然很想知道其他人是否认为这是正确的方法?
To answer my own question, I added
MKAnnotationView* annotationView = nil;
to the top of my- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation
code.Now it seems to be working perfectly. I'd still be keen to see if others think that this is the correct way to do this?
我解决了检查注释视图上的子视图的类似问题。
如果它没有子视图,我创建一个新标签并将其添加为子视图,如果它有子视图,我采用第一个标签并编辑 label.text 属性。
这样子视图总是零或只有一个。
I solved a similar problem checking subviews on annotation view.
if it hasn't subviews i create a new label and add it as subview, if it has subviews i take the first and edit the label.text property.
In this way subviews are always zero or only one.