简体   繁体   中英

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 center 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM