4
votes

I have a UIViewController that loads a map with a bunch of pins already on it. There's no issues getting the pins to appear, but I can't seem to get MKMapView to execute setRegion when the user first reaches the mapView. I've tried putting the code in every possible method that could work, viewDidLoad, viewWillLoad, mapViewWillStartLoadingMap, mapViewDidFinishLoadingMap, etc., to no avail.

And yet, it WILL "zoom" to the designated region if I return to the previous viewController and then go into the viewContoller containing the mapView again.

Is there a super secret way of doing this that I'm not aware of? Shouldn't calling [mapView setRegion:(...) animated:YES]; be enough to get the map to "zoom"/update to show the chosen region??

Also, if anybody knows how to get the pins (I'm using MKPointAnnotation for mine) to animate dropping in as well as getting the map to animate zooming in, (neither are doing it, although I set both to animate....), I'd appreciate that info as well.

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];
    }
1
@J2theC Added the code as requested - jpcguy89
When this view controller is first shown, what does the NSLog print for the coordinates? When first shown, is the map showing the Atlantic Ocean? If you hard-code coordinates into the setRegion (instead of reading from NSUserDefaults) does it work? Where are the NSUserDefaults setDouble calls done? - user467105
@AnnaKarenina IT prints the correct coordinates, (I just checked them). Yes, it shows the location as being in the north atlantic, I set it in my main view controller which is several scenes behind and the first loaded, hard-coding ends in the same result, first load is to north atlantic, and finally, I was having issues getting CLLocationManager to give me the "users" (my own) coords until I placed the code in my first view, (main menu) which is several segues aways from the mapView which is where I retrieve the coordinates from NSUSerDefaults. - jpcguy89

1 Answers

0
votes
- (void) viewDidAppear:(BOOL)animated {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(firstPoint.coordinate, 2000, 2000);
    [mapView setRegion:region animated:YES];
}

This works for me.