How can I know when MKMapview setRegion:animated: has finished?

10,591

Solution 1

Never used mapkit but the MKMapViewDelegate has a method mapView:regionDidChangeAnimated: that looks to be what you're looking for.

Be aware that mapView:regionDidChangeAnimated: will get called every time there's a change, such as when the user moves the map.

Solution 2

I know this is super old, but just in case anyone else comes by looking for an answer, here's an alternative.

The nice thing about this version is that you can run a completion animation at the exact moment the first one is complete instead of guessing/hardcoding it in the callback method since that one is called right away.

[MKMapView animateWithDuration:1.0 animations:^{
    [mapView setRegion:mapRegion animated:YES];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:1.0 animations:^{
        self.mapDotsImageView.alpha = 1.0;
    }];
}];

or just

// zoom in...
let km3:CLLocationDistance = 3000
let crTight = MKCoordinateRegionMakeWithDistance(location.coordinate, km3, km3)
MKMapView.animate(withDuration: 1.0, animations: { self.theMap.region = crTight })
Share:
10,591

Related videos on Youtube

deadroxy
Author by

deadroxy

Computer Whisperer (PhD). Formerly CEO & Co-Founder of frestyl.

Updated on April 17, 2022

Comments

  • deadroxy
    deadroxy about 2 years

    I want to set a region on my MKMapView and then find the coordinates corresponding to the NE and SW corner of the map.

    This code works just fine to do that:
    //Recenter and zoom map in on search location
    MKCoordinateRegion region =  {{0.0f, 0.0f}, {0.0f, 0.0f}};
    region.center = mySearchLocation.searchLocation.coordinate;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [self.mapView setRegion:region animated:NO]; //When this is set to YES it seems to break the coordinate calculation because the map is in motion
    
    //After the new search location has been added to the map, and the map zoomed, we need to update the search bounds
    //First we need to calculate the corners of the map so we get the points
    CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
    CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));
    
    //Then transform those point into lat,lng values
    CLLocationCoordinate2D neCoord;
    neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
    CLLocation *neLocation = [[CLLocation alloc] initWithLatitude:neCoord.latitude longitude:neCoord.longitude];
    
    CLLocationCoordinate2D swCoord;
    swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
    CLLocation *swLocation = [[CLLocation alloc] initWithLatitude:swCoord.latitude longitude:swCoord.longitude];
    

    The problem is I would like the map zoom to be animated. However, when I set the setRegion:animated to YES, I end up getting the coordinates from the map when it's zoomed way out (i.e., before the animation is completed). Is there any way to get a signal that the animation is done?

  • deadroxy
    deadroxy over 14 years
    Stupid me, it was in the delegate protocol not the MKMapView itself. I knew I had seen it somewhere... thanks :D
  • jason d
    jason d almost 3 years
    Nice and clean solution. Thanks.