3
votes

i have a master-detail app set up with the master being a UITableView containing a list of listings, each of which has a location (latitude and a longitude). when the user taps a list item, they get a detail view comprising an MKMapView, with the relevant annotations on it.

the code for setting the region to be displayed is below. however, the first time the user taps an item on the list, the mapView shows half the world, centered apparently on 0,0. the annotation that is placed (code not shown) is in the appropriate place, but the center and scale are all wrong. if the user returns to the master list and taps the item again (or taps any other item), the map is properly centered and sized, and the code works v. i cannot determine why the mapView is not responding the setRegion call properly the first time.

the code below is in the viewWillAppear method of the detail controller, but the effect is the same if it is put in viewDidLoad, or even in the prepareForSegue method of the master view controller.

i also tried changing the initial declaration of region to different coordinates, thinking that might be the problem, but it isn't...the behavior is the same no matter what values i use when initializing region, or even if i don't set it's initial values.

another SO user has asked the same question (Cannot get UIMapView to "zoom"/setRegion: when the map is loaded for the first time) but none of the answers solve the problem.

fwiw, the detail view controller is declared to be an MKMapView delegate, and is assigned as the mapView's delegate, but i have not implemented any of the delegate methods (it is also an MKAnnotation delegate and i have implemented mapView:viewForAnnotation)

any guidance about where i need to put this code, or what kind of delegate method i need to implement, so that it will cause the appropriate centering and scale the first time the mapView appears would be much appreciated.

here's the code:

 CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([self.listing.latitude doubleValue], [self.listing.longitude doubleValue]);
MKPointAnnotation *selectedListingAnnotation=[[MKPointAnnotation alloc] init];
selectedListingAnnotation.coordinate=coordinates;

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center = coordinates;
region.span.longitudeDelta = 0.02f;
region.span.latitudeDelta  = 0.02f;
[self.mapView setRegion:region animated:YES];
if (selectedListingAnnotation) {
    [self.mapView addAnnotation:selectedListingAnnotation];
}
1

1 Answers

2
votes

In viewDidLoad method, after adding annotations to the mapview, use the following code for centring,

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([self.listing.latitude doubleValue], [self.listing.longitude doubleValue]);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.18;
span.longitudeDelta=0.18;    
region.span=span;
region.center=coordinate;
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];