Greg
Greg

Reputation: 276

map does not center on annotation pin on first attempt

When a user selects a menu option I currently have the location name sent to my map view via "stringToDisplay" and a pin drops on the map as expected, however, it does not center the pin or zoom on the location on the first attempt. Every attempt after the first is flawless. How can I make the map zoom to my location(s) on the first attempt? Your input is greatly appreciated.

I have multiple IF statements contained in my viewWillAppear, but see below for one example of a location which experiences this problem:

-(void)viewWillAppear:(BOOL)animated

{
    CLLocationCoordinate2D location;

    NSLog(@"self.stringToDisplay = %@", self.stringToDisplay);

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.1;
    span.longitudeDelta=0.1;

    region.span=span;
    region.center=location;

    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];

  if ([self.stringToDisplay isEqualToString: @"August First Bakery & Café"])

  {
    location.latitude = (double) 44.475486;
    location.longitude = (double) -73.2172641;

    MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"August First Bakery & Café" andCoordinate:location];
    [self.mapView addAnnotation:newAnnotation];
    [mapView setCenterCoordinate:location animated:YES];
  }
}

Upvotes: 0

Views: 1938

Answers (1)

Greg
Greg

Reputation: 276

Set a breakpoint in the viewWillAppear. Check to see if mapView is nil when you're doing the setRegion/regionThatFits. If it is you may need to move the code to viewDidAppear. - compliments of Phlibbo

Upvotes: 1

Related Questions