2012-10-14 13 views
3

我設置了mapView.delegate
一切都很好,但是當我離開,幾分鐘後我恢復我的應用程序,它退出。MapKit終止應用程序,由於未捕獲的異常無效的區域

控制檯給我,但我一直沒能解決它:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'Invalid Region <center:-180.00000000, -180.00000000 span:+0.09000000, +0.09000000>' 

didUpdateUserLocation方法發送setRegion用戶位置進行了更新後

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    [mapView removeAnnotations: [mapView annotations]]; 
    for (TOJSearchItem *searchItem in _searchItems) { 
     TOJPlaceRequest *request = [TOJPlaceRequest new]; 
     [request execute:SERVER_URL coordinate:userLocation.coordinate searchItem:searchItem delegate:self]; 
    } 
    MKCoordinateRegion region = {{userLocation.coordinate.latitude , userLocation.coordinate.longitude}, {0.09f , 0.09f}}; 
    [mapView setRegion:region animated: YES]; 
} 

化妝要求

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    if (_lastLocation != nil) { 
     CLLocationDistance meters = [newLocation distanceFromLocation:_lastLocation]; 
     if (meters < DISTANCE_MINI) { 
//   CALCUL tout les points par rapport a current location 
//   NSLog(@"%f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude); 
     } else { 
      for (TOJSearchItem *searchItem in _searchItems) { 
       TOJPlaceRequest *request = [TOJPlaceRequest new]; 
       [request execute:SERVER_URL coordinate:newLocation.coordinate searchItem:searchItem delegate:self]; 
      } 
      MKCoordinateRegion region = {{newLocation.coordinate.latitude , newLocation.coordinate.longitude}, {0.09f , 0.09f}}; 
      [_mapView setRegion: region animated: YES]; 
      NSLog(@"reconnection"); 
     } 
    } 
    _lastLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]; 
} 

點擊一個註釋後重置該區域ation

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    if ([view class] == TOJClusterPushPinView.class) { 
     TOJClusterPushPinView *clusterPushPinView = (TOJClusterPushPinView *)view; 
     CLLocationCoordinate2D topLeftCoordinate; 
     topLeftCoordinate.latitude = -90; 
     topLeftCoordinate.longitude = 180; 
     CLLocationCoordinate2D bottomRightCoordinate; 
     bottomRightCoordinate.latitude = 90; 
     bottomRightCoordinate.longitude = -180; 
     for (TOJPushPin *pushPin in clusterPushPinView.clusterPushPin.pushPins) { 
      topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, pushPin.coordinate.longitude); 
      topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, pushPin.coordinate.latitude); 
      bottomRightCoordinate.longitude = fmax(bottomRightCoordinate.longitude, pushPin.coordinate.longitude); 
      bottomRightCoordinate.latitude = fmin(bottomRightCoordinate.latitude, pushPin.coordinate.latitude); 
     } 
     MKCoordinateRegion region; 
     region.center.latitude = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 0.5; 
     region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 0.5; 
     region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 2.0; // Add a little extra space on the sides 
     region.span.longitudeDelta = fabs(bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 2.0; // Add a little extra space on the sides 
     region = [mapView regionThatFits:region]; 
     [mapView setRegion:region animated:YES]; 
    } 
} 
+0

什麼是「當我離開」的意思是(流行VC,解僱VC,其他)?顯示代碼在哪裏調用setRegion。 – Anna

+0

當我切換應用程序(無論ap),我回到這個應用程序,它退出併發送給我在控制檯上的錯誤 – james075

+0

嘗試應用[這個問題]的建議(http://stackoverflow.com/questions/8394969/unrecognized-error -in-MapView類-IOS)。另請參閱ng32rg對該問題的回答。你的情況最有用的檢查可能是'!(!CLLocationCoordinate2DIsValid(center))'。 – Anna

回答

0

如果你的didUpdateToLocation方法,你應該檢查你給定的位置變量是否有效。

if (userLocation.location.horizontalAccuracy >= 0) { 

衆所周知的行爲,但我沒有看到它的邏輯,它簡歷後獲得的第一個位置可能是無效的。應該丟棄任何精度爲< 0的位置。

編號:http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/occ/instp/CLLocation/horizontalAccuracy

+0

它不起作用,總是在恢復應用程序後退出 – james075

+0

您是否已放入任何斷點或日誌語句以查看它是否爲didUpdateUserLocation或didUpdateToLocation?我在你的回答中看到(爲什麼你回答自己的問題,說你顯示的答案不起作用?)只顯示對didUpdateToLocation的更改。 – Craig

+0

我把條件檢查和didUpdateUserLocation上的一個斷點,這裏是問題它終止在這條線上:\t \t MKCoordinateRegion region = {{userLocation.coordinate.latitude,userLocation.coordinate.longitude},{0.09f,0.09f} }; \t \t [mapView setRegion:region animated:YES]; – james075

0

變化

if (userLocation.location.horizontalAccuracy >= 1) { 

....... 

} 

它的工作原理

0

你應該如果

userLocation.location != nil 

在我的應用程序檢查didUpdateUserLocation,從applicationDidBecomeActive事件返回時,第一個座標總是無效的。

相關問題