Search code examples
iosxcodeios6mkmapviewxcode4.5

iOS MKMapView starts zoomed out default map but zooms in only on refresh


Everytime I start the app, the first time seeing the map results in a default map that is always zoomed out with no annotations. When I go back on the navigation controller and go back into the map, it now shows the correct region with the appropriate pins. The code I use to add the

- (void) zoomIn {
    mapView.showsUserLocation = YES;

    CLLocationCoordinate2D annotation;
    annotation.latitude = 47.640071;
    annotation.longitude = -122.129598;

    MKPointAnnotation *annoPoint = [[MKPointAnnotation alloc] init];
    annoPoint.coordinate = annotation;
    annoPoint.title = @"name";
    [mapView addAnnotation:annoPoint];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(annotation, 500, 500);

    [mapView setRegion:region animated:YES];
}

I call this block of code from the viewDidLoad, but it only works after I go back to the main page from the navigation controller and enter this UIViewController again.

Does anyone know what the problem is or have seen it before?


Solution

  • - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        // this delegate fonction is called when the userlocation is updated
        // try to move your code here
    }
    

    you have also

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
    {
    }
    

    hope this helps