0
votes

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?

1
did you set your view controller to be the map's delegate?bennythemink
I assume you're selecting it so that you can see the callout? Then make sure to write a viewForAnnotation that enables the callout.Rob

1 Answers

1
votes

didAddAnnotationViews isn't being called at all

In your viewDidLoad implementation, add this line:

self.map.delegate = self;

See if that changes things.