2012-10-02 28 views
1

剛剛感受到iOS 6特有的這個奇怪問題。 我使用以下代碼在iPhone地圖中固定給定的一組地址,它工作正常IOS 4和5。但是崩潰在iOS 6以下堆棧跟蹤運行時,iOS 6使用無效參數調用「setRegion」時地圖崩潰

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:+0.00000000, +0.00000000 span:+177.61462012, +900.00000000>'

我使用的代碼是簡單,因爲它可以是

`CLLocationCoordinate2D topLeftCoord; topLeftCoord.latitude = -90; topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord; 
bottomRightCoord.latitude = 90; 
bottomRightCoord.longitude = -180; 

for (id<MKAnnotation> annotation in self.mapView.annotations) { 
    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
} 
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.5; 
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.5; 

region = [self.mapView regionThatFits:region]; 
[self.mapView setRegion:region animated:YES]; 

`

所以,問題顯然與計算longitudeDelta我相信,因爲它試圖訪問一個錯誤的經度900.000。因此,我改變了

上面的代碼

region.span.latitudeDelta= self.mapView.region.span.latitudeDelta /2.0002; region.span.longitudeDelta= self.mapView.region.span.longitudeDelta /2.0002;

和碰撞吸能得到解決,但地圖點,在世界上不同的位置。希望可以擺脫這個

+0

你的問題是減去幾個數字。在NSLog語句中添加一些中斷點並查看最後一行給你的錯誤數字。將每個元素分解爲局部變量,它也會變得更簡單。 – Jessedc

+0

我認爲prob是regionThatFits正在調用一個0區域 –

回答

1

一些專家的知識,我覺得你得到它:

「的問題顯然是與計算longitudeDelta」

我經歷了同樣的事情,我要做的就是簡單:

-(void)myFunction 
{ 
    MKCoordinateRegion region; 
    region.span.longitudeDelta = 10; 
    region.center = CLLocationCoordinate2DMake(46, 2); 
    [map setRegion:region]; 
} 

它崩潰的第二或myFunction第三個呼叫後,但是當我使用latitudeDelta而不是longitudeDelta,它完美地工作

此外,崩潰日誌是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
    reason: 'Invalid Region <center:+46.00000000, +2.00000000 span:-1.99143220, +10.00000000>' 

這讓我覺得longitudeDelta屬性相當竊聽。

PS:無需設置longitudeDelta和latitudeDelta因爲longitudeDelta = F(latitudeDelta)

0

當然它崩潰。

  1. 當只有1
  2. 當只有N個等位置標註

只考慮案件的算法,在這種情況下未能有一個備份區。

例如

if(noAnnotation) setRegion: world 
if(annotationCount < 2) setRegion: region for annotation1 (with a span of e.g. 0,05/0,05) 
else 
    ... Do the algo 
    if longitudeDelta == 0 setRegion: region for annotation1 (with a span of e.g. 0,05/0,05) 
    Else setRegion: like you do now