8

The regular annotation pin's origin is in the middle of the bottom so, the pin always point to the same place.

But when I add my custom image, its origin is the center of the image, so every zoom in or out, the bottom of my image point to a different place.

enter image description here

Here my pin is supposed to point to the center of paris BUT

enter image description here

but when I zoom in, the bottom of my pin isn't pointing to the center of Paris.

I'm trying with the CGRect.origin but didn't get anything useful.

Here is my code:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView * customPinView = [[MKAnnotationView alloc] init];
    UIImage * img = [UIImage imageNamed:@"waterPin.png"] ;
    CGRect resizeRect;
    resizeRect.size.height = 40;
    resizeRect.size.width = 40;
    resizeRect.origin = (CGPoint){0.0f, 0.0f};
    UIGraphicsBeginImageContext(resizeRect.size);
    [img drawInRect:resizeRect];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    customPinView.image = resizedImage;
    return customPinView;
}
AMTourky
  • 1,190
  • 13
  • 25
  • if you could post some code?? – Dinesh Raja Nov 21 '12 at 12:24
  • 1
    What coordinates (lat/long) is the annotation set to exactly? Try setting `customPinView.centerOffset` to adjust the image offset (but I'd verify the coordinates first). Also, you should use initWithAnnotation instead of just init for MKAnnotationView. –  Nov 21 '12 at 13:44
  • no no, the coordinates is right, but it's that little property, centerOffset, thanks a lot – AMTourky Nov 21 '12 at 14:17
  • hello can you tell me how can we replace the default pin with "waterPin.png" . – 08442 Jan 02 '13 at 14:18
  • it is in code above, just get your image, and assign it to the image property of the annotation img = .....; pin.image = img; – AMTourky Jan 03 '13 at 07:31

1 Answers1

13

MKAnnotationView has a centerOffset property which you can try setting to adjust the image offset:

customPinView.centerOffset = CGPointMake(xOffset,yOffset);


Unrelated, but you should use initWithAnnotation instead of just init for creating an MKAnnotationView.
It also wouldn't hurt to use dequeueReusableAnnotationViewWithIdentifier and implement annotation view re-use to improve performance.

I would also suggest not programmatically resizing the image in the delegate method and instead using an already-resized image to begin with. Then you can just do customPinView.image = [UIImage imageNamed:@"resizedWaterPin.png"]; without spending run-time resizing the annotation images every time.

  • what about the dragging?! I set it draggable = YES, but not all the view, I need to click in some exact places to drag it. I want it to be dragged wherever I touch it – AMTourky Nov 22 '12 at 07:08
  • 1
    Make sure your annotation class implements setCoordinate otherwise it can't be dragged even if draggable is YES. See http://stackoverflow.com/questions/11927692/ios-mkmapview-draggable-annotations and [the documentation](http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW22). –  Nov 22 '12 at 14:09
  • it's dragging, yes I implemented the needed methods, but the pin itself, I need to touch them from specific locations, not any where of the pin – AMTourky Nov 25 '12 at 08:29
  • I'm sorry but I don't understand. You might want to start a new question with more details. –  Nov 25 '12 at 13:52