2010-01-17 168 views
18

我想在我的MKMapView上設置一個區域,然後找到對應於地圖的NE和SW角的座標。如何知道MKMapview setRegion:animated:已完成?

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]; 

問題是我想將地圖縮放爲動畫。但是,當我將setRegion:animated設置爲YES時,最終會在放大地圖時(即動畫完成之前)從地圖獲取座標。有什麼方法可以獲得動畫完成的信號?

回答

21

從未使用的mapkit,但MKMapViewDelegate有一個方法mapView:regionDidChangeAnimated:,看起來是你在找什麼。

+0

愚蠢的我來說,這是在委託協議不是的MKMapView本身。我知道我曾經見過它......謝謝:D – deadroxy 2010-01-17 19:47:03

4

我知道這是超舊的,但爲了防止其他人來尋找答案,這裏有一個選擇。

這個版本的好處在於,您可以在完成第一個完成動畫時運行完成動畫,而不是在回調方法中猜測/硬編碼,因爲該動畫立即被調用。

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

或只是

// zoom in... 
let km3:CLLocationDistance = 3000 
let crTight = MKCoordinateRegionMakeWithDistance(location.coordinate, km3, km3) 
MKMapView.animate(withDuration: 1.0, animations: { self.theMap.region = crTight }) 
相關問題