0
votes

I am trying put a pin on User's current location. Picking location from LocationManger and assigning it to MKAnnotation. But when I run the application, Map show users location and Pin location at different places on Map. Not very far but I am wondering why it's not same. Where as both custom pin and user's location are same on simulator.

Below is some of sample code.

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];

MyLocation *mylocation = [[MyLocation alloc] initWithName:@"name" address:@"address" coordinate:self.locationManager.location.coordinate] ;

Do I need to set some accuracy level? wondering how MKMapView is showing location.

1
You are probably placing the pin when the user's location is first updated. As you know, the location can bounce around until it gets the most accurate location of the user. You should place the pin in didUpdateLocations, so that the pin is always placed at the most accurate and recent user location.MSU_Bulldog

1 Answers

0
votes

Try this code:

#pragma mark -
#pragma mark MKMapView delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView)
        return annotationView;
    else
    {
        MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                         reuseIdentifier:AnnotationIdentifier] autorelease];
        annotationView.canShowCallout = YES;
        annotationView.image = [UIImage imageNamed:@"someImage.png"];
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        annotationView.rightCalloutAccessoryView = rightButton;
        annotationView.canShowCallout = YES;
        annotationView.draggable = YES;
        return annotationView;
    }
    return nil;
 }

for more detail prefer this