MKMapView Zooming In On User's Location Once But Not The Second Time in Tab-Bar App (iOS)

12,764

I would pull all of the zooming code into its own method that can be messaged from -viewDidAppear: and -mapView:didUpdateToUserLocation:.

- (void)zoomToUserLocation:(MKUserLocation *)userLocation
{
    if (!userLocation)
        return;

    MKCoordinateRegion region;
    region.center = userLocation.location.coordinate;
    region.span = MKCoordinateSpanMake(2.0, 2.0); //Zoom distance
    region = [self.mapView regionThatFits:region];
    [self.mapView setRegion:region animated:YES];
}

Then in -viewDidAppear:...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self zoomToUserLocation:self.mapView.userLocation];
}

And in the -mapView:didUpdateToUserLocation: delegate method...

- (void)mapView:(MKMapView *)theMapView didUpdateToUserLocation:(MKUserLocation *)location
{
    [self zoomToUserLocation:location];
}
Share:
12,764
MillerMedia
Author by

MillerMedia

Thanks for the help! Any programmers looking for subcontractor work, feel free to e-mail me at [email protected] . We're always looking for great workers. Thanks!

Updated on June 04, 2022

Comments

  • MillerMedia
    MillerMedia almost 2 years

    I have an MKMapView as part of a Navigation Controller in a Tab Bar based app.

    I click a UIButton on the first View Controller and it pushes to the second View Controller which contains the MKMapView. When the Map View loads, it zooms in on the user's location using:

    - (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        if ( !initialLocation )
        {
            self.initialLocation = userLocation.location;
    
            MKCoordinateRegion region;
            region.center = theMapView.userLocation.coordinate;
            region.span = MKCoordinateSpanMake(2.0, 2.0);
            region = [theMapView regionThatFits:region];
            [theMapView setRegion:region animated:YES];
        }
    }
    

    When I hit the back button on the Navigation Controller above the MapView and then click back to the map, it no longer zooms in on the user's current location, but just has the full zoom out default:

    Here's a picture of the view the second time.

    I figure it would work correctly if I could somehow call the didUpdateUserLocation in the viewDidAppear method but I'm not sure how to pull this off since the didUpdateUserLocation is a delegate method.

    Is that the right approach or is there a different approach I should take to do this? Thanks!

    P.S. I've seen this question but it's slightly different with it's use of a modal view controller

  • MillerMedia
    MillerMedia over 12 years
    Ah ok, that makes total sense. When I do run it though, it still isn't zooming in (even though it does make sense what you've written). It's still displayed the user location correctly but it's not zooming in on it. The error message I get is: "CoreAnimation: ignoring exception: 'Invalid Region <center:-180.00000000, -180.00000000 span:+2.81462803, +28.12500000>"
  • MillerMedia
    MillerMedia over 12 years
    It's not recognizing the region for some reason, although it hasn't changed from the original code.
  • Mark Adams
    Mark Adams over 12 years
    My mistake, I was trying to access the coordinate property on MKUserLocation when there isn't one. You need to get the coordinate from the location property on MKUserLocation. Updated my code.
  • MillerMedia
    MillerMedia over 12 years
    It creates the same problem. Does it have something to do with the "region = [theMapView regionThatFits:region]; [theMapView setRegion:region animated:YES];? In the original code, 'theMapView' was a pointer that was set in the didUpdateToUserLocation method. Right now I've got it set to mapView (which is the MKMapView object I synthesized for the overall class, not the particular method). Does that make sense? I have a feeling it's something to do with that...
  • Mark Adams
    Mark Adams over 12 years
    Ah, I've updated my code once again to use the synthesized mapView property.
  • MillerMedia
    MillerMedia over 12 years
    Oh ok thanks! (I may have some follow up questions if you'll humor haha, I'm trying to learn!) The one thing that happens now is that once I'm in the map view it zooms in. Then when I hit the navigation bar button to go back, it doesn't switch the view back. The button just disappears and displays the title of the first view controller but doesn't actually change the view. Thank you so much for your help (I hate being lead along but I'm so confused by the MapView right now and would like to finish this and then study the code)...
  • Mark Adams
    Mark Adams over 12 years
    That sounds like an entirely separate issue that to be honest is beyond the scope of this question. Post it as a new question.