USS1994
USS1994

Reputation: 614

Centering MKMapView on an annotation

Although the process for doing this is well documented, I am unable to solve this problem.

All I want to do is to take the coordinate at which I have just created an annotation and have the map zoom in and center on it. I followed this tutorial with some modifications: http://maybelost.com/2011/01/a-basic-mapview-and-annotation-tutorial/

The MapViewAnnotation class consists of just a title and the coordinates. _detailItem is simply an object that holds a name, latitude, and longitude as strings.

I have a basic map. I'm using a storyboard (so, ARC). I have coordinates. Here's my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];

    // Set some coordinates for our position
    CLLocationCoordinate2D location;
    double thisLat = [_detailItem.EventLat doubleValue];
    double thisLong = [_detailItem.EventLong doubleValue];

    location.latitude = (double)thisLat;
    location.longitude = (double)thisLong;

    // Add the annotation to our map view
    MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:_detailItem.EventName andCoordinate:location];
    [self.map addAnnotation:newAnnotation];
}

// When a map annotation point is added, zoom to it
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1000, 1000);
    [mv setRegion:region animated:YES];
    [mv selectAnnotation:mp animated:YES];
}

Yeah. When I do this, the annotations shows up as expected, but the map doesn't center on it or zoom in. During debug, I found that didAddAnnotationViews isn't being called at all! Yet, I've clearly made an explicit call to addAnnotation.

Help?

Upvotes: 0

Views: 308

Answers (1)

matt
matt

Reputation: 534885

didAddAnnotationViews isn't being called at all

In your viewDidLoad implementation, add this line:

self.map.delegate = self;

See if that changes things.

Upvotes: 1

Related Questions