开发者

MKAnnotationView Doesn't Stay In Place When Zooming

I'm placing custom markers on my map in iOS and I'm having a problem whereby when the user pinches to zoom in and out, the markers don't anchor to where they should. Here's the code that adds markers...

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

    MarkerVO *thisMarker = (MarkerVO*)annotation;

    MKAnnotationView *pin = (MKAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:[thisMarker commaSeparatedCoordinate]];
    if (!pin) {
        pin = [[[MKAnnotationView alloc] initWithAnnotation:annotation开发者_开发知识库 reuseIdentifier:[thisMarker commaSeparatedCoordinate]] autorelease];
        [pin setImage:[UIImage imageNamed:@"pin_tick.png"]];
        [pin setCenterOffset:CGPointMake(0, -23)];
        [pin setCanShowCallout:YES];
    }
    return pin;
}

So yes, the tick marker displays ok but on zoom, it just moves around. For example, it could be right on the spot at close zoom but zooming way out ends up with it being in the sea! I get the idea of why this is happening however even without the setCenterOffset line, it's still happening.

Any ideas would be great.


When pin does not return nil from the dequeue, try setting pin.annotation to annotation.

The re-used view might somehow be from a different annotation even though the code seems to be setting a unique identifier for each annotation (which I don't recommend in any case).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜