Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone custom pin position issue

Tags:

iphone

mapkit

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

like image 834
user349302 Avatar asked Jun 12 '11 03:06

user349302


People also ask

How do I pin an exact location on my iPhone?

You can mark places in the Maps app with pins to help you find those places later. Tip: To quickly mark your location so you can find your way back later, touch and hold the Maps icon on the Home Screen, then choose Mark My Location. See Perform quick actions on iPhone.

How accurate is pin drop on iPhone?

It's the same accuracy as the GPS accuracy of the device. If the GPS on the iPhone can't obtain a good signal, it may use Wi-Fi triangulation which will reduce the accuracy.

Why is there a random marked location on my iPhone?

Buried in your iOS location settings is a feature called "Frequent Locations." This is plotting the places you often go, including complete addresses and even a map. If something about that feels not great to you, here's how to find and disable it.

How do you move pins on iPhone?

You can rearrange the pinned conversations easily — simply drag a pinned conversation where you want it, then release. You'll see the other pins move around and out of the way.


2 Answers

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

like image 136
Aravindhan Avatar answered Nov 05 '22 17:11

Aravindhan


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;
}
like image 33
ikarius Avatar answered Nov 05 '22 19:11

ikarius