Search code examples
mkmapviewzoomingxcode4.5regionmkcoordinateregion

My mapview does not zoom on the first pass into the view controller


Can anybody help me please? I'm new to xcode and am baffled by what I think is a small problem. For some reason the first time my map is opened from my initial view controller the pin is dropped but the map does not zoom to the correct region even though the latitude and longitude are correct.

When I go back to the first view controller and launch it again the zoom works.

My code is in the view controllers implementation file under viewdidload.

-(void)viewDidLoad {

[super viewDidLoad];
NSLog(@"lat in map %@", eventLat);
NSLog(@"long in map %@", eventLong);
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];

MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = [eventLat doubleValue];
region.center.longitude = [eventLong doubleValue];
region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;

[mapview setRegion:region animated:YES];

MMAnnotate *ann = [[MMAnnotate alloc] init];
ann.title = @"Test Title";
ann.subtitle = @"Test Subtitle";
ann.coordinate = region.center;
[mapview addAnnotation:ann];

}


Solution

  • You should try doing that in viewDidAppear instead. If you're prepared for it to zoom to that location every time the view appears. viewDidLoad is too early in the mapview cycle.