6

I've been playing around with the MKMapView and trying get my head around how the MKMapViewDelegate system works. So far I have no luck in getting the didAddAnnotationViews to get called when the current location marker is added.

I have set my app delegate to implement MKMapViewDelegate, I have an Outlet to the MapView in my xib and have set the delegate property of the MapView to be self, as in the app delegate instance. I have implemented didAddAnnotationViews in the app delegate which I simply NSLog any calls to it as shown below. The map is set to show current location which it does and adds the blue pin annotation on startup, but for some reason didAddAnnotationViews is not being hit.

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    NSLog(@"Annotation added!");
}

Any ideas what I might have missed?

BenBtg
  • 836
  • 1
  • 9
  • 22

3 Answers3

5

I came across the same issue in BNR. Here is what I ended up using:

    // Tell MKMapView to zoom to current location when found
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation just got called!");

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 250, 250);
    [mapView setRegion:region animated:YES];
}
Maulik
  • 19,348
  • 14
  • 82
  • 137
user656888
  • 51
  • 2
3

mapView:didAddAnnotations: is only called in response to addAnnotation: or addAnnotations:. The users location pin will not trigger this delegate method.

Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • 1
    Thanks, are you sure about that? I did wonder if this was the case but I have been following the WhereAmI tutorial in the Big Nerd Ranch book on iPhone programming and it states quite clearly that the didAddAnnotationViews will be called when the blue dot represent current location is added. In fact it provides a block of code to use in didAddAnnotationsView to set the current region to zoom on your current location. That seems like a huge blunder on the authors part. – BenBtg Jan 30 '11 at 00:17
  • Actually, a bit of Googling has confirmed that you are right and Apple changed the behaviour in IOS 4.0! Apparently I now need to use didUpdateUserLocation. – BenBtg Jan 30 '11 at 00:23
0

Just wanted to confirm that I was able to get this working using

 - (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id <MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250,250);
    [mv setRegion:region animated:YES];
}

Make sure you are using mapView.delegate = self; or [mapView setDelegate:self];

James Parker
  • 2,095
  • 3
  • 27
  • 48