开发者

iPhone custom pin position issue

I have an application that uses iphone map kit. It shows set of standard pins. Now I switch to custom images for pins (using MKMapViewDelegate)

The problem is that custom pins are not centered right - custom pin pointing to a location near original.

The question is how iphone works with custom pin. For example: let's have a image for pin 50x50 pixels. And we have global coordinate (long, lat): XY

How will iphone cent开发者_如何学编程er image?

thank you


If you assign your custom image to the image property. When the annotation is displayed, the image is displayed centered over the target map coordinate. If you do not want the image to be centered on the map coordinate, you can use the centerOffset property to move the center point horizontally and vertically in any direction.

So the custom image is displayed in the center of your targeted coordinates only.

MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation

                                  reuseIdentifier:@"MyCustomAnnotation"] autorelease];

aView.image = [UIImage imageNamed:@"myimage.png"];

aView.centerOffset = CGPointMake(10, -20);     

Source: apple class reference for MKMapView


The solution is to set center offset in the delegate of the mapView, and not in the annotation view it self:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString* const kPinIdentifier = @"pinidentifier";

    PinAnnotationView* customPinView = [[[PinAnnotationView alloc]
                                           initWithAnnotation:annotation reuseIdentifier:kPinIdentifier] autorelease];
    customPinView.canShowCallout = NO;

    // Here !!
    customPinView.centerOffset = CGPointMake(0, -21.5f);

    return customPinView;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜