user2113892
user2113892

Reputation: 13

Annotations don't appear when setting MainViewController as delegate of MKMapView

I have a funky problem I'm dealing with… i have a very basic mkmapview that i dragged into the mainviewcontrollers view. I have very basic annotations that i am showing on the map. When I make the mainviewcontroller the delegate (in IB or code) of the mapview, the annotations stop showing up. If i don't make this connection, the annotations appear as expected… any ideas?

#import "GDFlipsideViewController.h"
#import <MapKit/MapKit.h>

@interface GDMainViewController : UIViewController <MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet UIToolbar *bottomToolbar;
@property (strong, nonatomic) IBOutlet MKMapView *theMap;
@property (strong, nonatomic) MKLocalSearchResponse *results;
@property (strong, nonatomic) MKLocalSearchRequest *request;
@property (strong, nonatomic) MKLocalSearch *search;

- (IBAction)locateHelp:(UIBarButtonItem *)sender;

@end
...

@implementation GDMainViewController
...
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *AnnotationViewID = @"annotationViewID";
    GDHelpAnnotationView *annotationView = (GDHelpAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil){
        annotationView = [[GDHelpAnnotationView alloc]init];

    }

    annotationView.annotation = annotation;

    return annotationView;
}

...

- (IBAction)locateHelp:(UIBarButtonItem *)sender {

    [self searchForHelp];
}

..

- (void)searchForHelp {
    // drop pins for emergency destination
    [self searchForFireStations];
    [self searchForPoliceStations];
    [self searchForHospitals];

}

...

- (void)searchForFireStations {
    //Fire stations
    [_request setNaturalLanguageQuery:@"fire station"];
    [_request setRegion:[_theMap region]];
    _search = [[MKLocalSearch alloc]initWithRequest:_request];

    [_search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        if ([response.mapItems count] == 0) {
            if ([response.mapItems count] == 0) {
                //will handle later
                return;
            }
        }

        [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {
            GDHelpAnnotation  *annotation = [[GDHelpAnnotation alloc] initWithPlacemark:item.placemark];
            annotation.title = item.name;
            annotation.subtitle = [item name];

            [_theMap addAnnotation:annotation]; // This gets called but doesnt show annotations when self is _theMap's delegate
        }];



    }];
}

Upvotes: 1

Views: 516

Answers (1)

Craig
Craig

Reputation: 8304

If viewForAnnotation is being called when you set your delegate, but the pins aren't appearing, the fault probably lies with GDHelpAnnotationView. You could test this by replacing your viewForAnnotation with this even more basic one that uses a regular old MKPinAnnotationView instead.

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil)
    {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    } else {            
        annotationView.annotation = annotation;
    }
    return annotationView;
}

Upvotes: 1

Related Questions