@class ShowsContainerC" /> @class ShowsContainerC" /> @class ShowsContainerC"/>

Can't get viewForAnnotation to fire after initial user location firing

339 Views Asked by At

I have this code:

// MapViewController.h
#import <Foundation/Foundation.h>
#import "MyCLController.h"
#import <MapKit/MapKit.h>

@class ShowsContainerController;

@interface MapViewController : UIViewController <MyCLControllerDelegate,MKMapViewDelegate> {
    MyCLController *locationController;
    MKMapView *mapView;
}

- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (assign) BOOL updateMap;
@property (strong) ShowsContainerController *parent;

@end

// MapViewController.m
#import "MapViewController.h"
#import "ShowsContainerController.h"

#define METERS_PER_MILE 1609.344

@implementation MapViewController

@synthesize parent, mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.updateMap = YES;
    locationController = [[MyCLController alloc] init];
    locationController.delegate = self;
    [locationController.locationManager startUpdatingLocation];
    mapView.delegate = self;
}

- (void)locationUpdate:(CLLocation *)location {

    if( self.updateMap ){
        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

        [mapView setRegion:viewRegion animated:YES];

        self.parent = (ShowsContainerController *)self.parentViewController;

        [self.parent setLocation:location];

        self.updateMap = NO;
    }
}

- (void)locationError:(NSError *)error {
    NSLog([error localizedDescription]);
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.parent.mapReady = YES;

    if( self.parent.locationSet ){
        [self.parent callApi];
    }
}

#pragma mark MKMapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"delegating user location");
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSLog(@"delegating pins");
    static NSString* ShowAnnotationIdentifier = @"showAnnotationIdentifier";

    MKPinAnnotationView* pinView = (MKPinAnnotationView *)
    [mapView dequeueReusableAnnotationViewWithIdentifier:ShowAnnotationIdentifier];

    if (!pinView)
    {
        // if an existing pin view was not available, create one
        MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc]
                                              initWithAnnotation:annotation reuseIdentifier:ShowAnnotationIdentifier];
        customPinView.enabled = YES;
        customPinView.pinColor = MKPinAnnotationColorRed;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;

        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

#pragma mark -

@end

And here's the code adding my pins; it isn't fired until an api call is made, which is after viewDidAppear is fired:

- (void)placeAnnotations:(NSArray *)shows
{

    NSEnumerator *e = [shows objectEnumerator];
    id show;
    NSMutableArray *annotations = [[NSMutableArray alloc] init];

    while (show = [e nextObject]) {
        double lat = [[[[[show objectForKey:@"venue"] objectForKey:@"location"] objectForKey:@"geo:point"] objectForKey:@"geo:lat"] doubleValue];
        double lng = [[[[[show objectForKey:@"venue"] objectForKey:@"location"] objectForKey:@"geo:point"] objectForKey:@"geo:long"] doubleValue];

        ShowAnnotation *annotation = [[ShowAnnotation alloc]
                                      initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
                                      withMap:self.mapController.mapView
                                      withTitle:[show objectForKey:@"title"]
                                      withSubtitle:[[show objectForKey:@"venue"] objectForKey:@"name" ]];
        [annotations addObject:annotation];
    }

    NSLog(@"adding annotations");
    [self.mapController.mapView addAnnotations:annotations];

}

The log shows "delegating user location", but never "delegating pins". It doesn't even fire viewForAnnotation more than the one initial time for the user's location.

The delegation seems to be working, otherwise I wouldn't be able to capture this selector, but why is it firing for only the user location and not firing for subsequent annotation additions?

0

There are 0 best solutions below