I have a UIViewController that loads a map with an existing bunch of pins. There are no problems with the appearance of contacts, but I can not get MKMapView to execute setRegion when the user first reaches mapView. I tried putting the code in any possible method that could work, viewDidLoad , viewWillLoad , mapViewWillStartLoadingMap , mapViewDidFinishLoadingMap , etc., to no avail.
And yet it will βzoom inβ to the specified area if I go back to the previous viewController and then again enter the viewContoller containing the mapView.
Is there a secret way to do this that I don't know about? Do not call [mapView setRegion:(...) animated:YES]; to get a map for "zoom" / update to display the selected area.
Also, if anyone knows how to get the pins (I use MKPointAnnotation for mine) to animate, and also get a map for the zoom animation (neither do they, although I set both to animate ....), I was I would be grateful for this information.
Thanks!
Code:
@implementation FindOnMapMapView @synthesize mapView; @synthesize mapViewTabBarItem; @synthesize results; @synthesize reverseGeocoder; - (void)viewDidLoad { [super viewDidLoad]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake([defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]), MKCoordinateSpanMake(0.744494189288569, 0.744494189288569)); mapView.region = mapRegion; [mapView setRegion:mapRegion animated:YES]; NSMutableArray *annotations = [[NSMutableArray alloc] init]; for(ThePlace *place in results) { MKPointAnnotation *addAnnotation = [[MKPointAnnotation alloc] init]; [addAnnotation setCoordinate:CLLocationCoordinate2DMake(place.latitude, place.longitude)]; addAnnotation.title = place.placename; addAnnotation.subtitle = [NSString stringWithFormat:@"Distance: %@", place.distance]; [mapView addAnnotation:addAnnotation]; } [self.mapView addAnnotations:annotations ]; } - (void)viewWillAppear:(BOOL)animated { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake([defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]), MKCoordinateSpanMake(0.744494189288569, 0.744494189288569)); NSLog(@"Region Coords are: Latitude:%f, Longitude:%f", [defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]); mapView.region = mapRegion; [mapView setRegion:mapRegion animated:YES]; }
source share