Question

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.

Was it helpful?

Solution

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?

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top