sayzlim
sayzlim

Reputation: 275

mapView:didAddAnnotationViews: doesn't respond

I am currently reading the iPhone Programming The Big Nerd Ranch Guide and encounter some problem on page 96.

I have assigned the MKMapViewDelegate properly (Tested with several delegates) and each of them respond to the change made. But during this delegate:

- (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:TRUE];
    NSLog(@"Test");
}

The console should log that when the annotation is added. But somehow it doesn't run the method above.

This is the main file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    //[locationManager startUpdatingLocation];
    [mapView setShowsUserLocation:TRUE];

    [self.window makeKeyAndVisible];

    return YES;
}

I wonder which part is wrong. Does the [mapView setShowUserLocation:TRUE] add annotation to the map? If it is yes, how come the there is no response from the method above?

Upvotes: 0

Views: 982

Answers (2)

James Bedford
James Bedford

Reputation: 28982

This is because you haven't added an annotation yet! The blue indicator that show's the user's location isn't classed as an annotation. You can add an annotation by simply dropping a red pin onto the map view (touch and hold the map).

If you want to create your own annotations for the map view programatically, I suggest you start by checking out the section "Annotating the Map" in the MKMapView class reference documentation and looking at the MKAnnotation protocol reference (if you're unfamiliar with protocols I strongly you suggest you read up on those too!) You can use an instance of the class MKPinAnnotationView, which conforms to this protocol, and you can add an annotation using the addAnnotation: instance method of the MKMapView class.

Upvotes: 1

Sig
Sig

Reputation: 5188

Checkout Custom curent location Annotation pin not updating with core location I answered something similar there before

Upvotes: 0

Related Questions