user349302
user349302

Reputation: 3821

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

Upvotes: 7

Views: 4958

Answers (2)

ikarius
ikarius

Reputation: 513

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

Upvotes: 6

Aravindhan
Aravindhan

Reputation: 15628

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

Upvotes: 9

Related Questions