Petar
Petar

Reputation: 2283

Map view viewForAnnotation called after a delay

I am implementing a functionality in my iPad app to show a mapview with custom annotations in it. For some reason, the - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation method gets called 1-2 seconds after the map has loaded, which I think is a lot of time.

The problem is that, if you navigate to another view from the mapview before the pins have loaded, the app crashed with bad access. Therefore, I assume this 1-2 second delay should not exist.

Nothing special is going on, there are only a few (3) pins to display so it should not be a performance problem.

Please let me know if you have a solution to that.

The code:

MAPVIEWCONTROLLER

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:self.pageTitle];

    [self.navigationController.navigationBar setTintColor:[UIColor lloydsNavBarTintColour]];
    [self.mapView setDelegate:self];

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = 53.566414;
    region.center.longitude = -0.922852;
    region.span.longitudeDelta = 10.0f;
    region.span.latitudeDelta = 10.0f;
    [self.mapView setRegion:region animated:YES];


    ACNEfficiencyAnnotation *annotation = [[ACNEfficiencyAnnotation alloc]init];

    [annotation setCoordinate:CLLocationCoordinate2DMake(55.5783462524414,-4.39453077316284)];

    [self.mapView addAnnotation:annotation];
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    self.annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];

    return self.annotationView;
}

ANNOTATION IMPLEMENTATION:

@interface ACNEfficiencyAnnotation : NSObject <MKAnnotation>
{

}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, strong) ACNEfficiencyLocation *location;

@end


@implementation ACNEfficiencyAnnotation

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize location;

@end

Upvotes: 0

Views: 545

Answers (2)

Raesu
Raesu

Reputation: 310

You are setting the property self.annotationView, and creating a new MKAnnotationView every time the delegate is called. Instantiate the property in viewDidLoad and just return self.annotationView in the delegate method.

Upvotes: 0

A-Live
A-Live

Reputation: 8944

You should use dequeueReusableAnnotationViewWithIdentifier to prepare the annotation at the delegate method rather then use the viewController's property to be reused.

There must be a problem with memory management at your viewController, check that you are releasing and setting the map view to nil when the view controller is released and it's view is unloaded.

Upvotes: 0

Related Questions