2011-08-26 37 views
1

我試圖隱藏一個經緯度數組來協調併爲其設置註釋。這樣做的應用程序崩潰。我在哪裏做錯了?不知何故,當數據變成雙倍時,數據就變得混亂了。將NSArray轉換爲CLLCoordinate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     CLLocationCoordinate2D addressCoordinate; 
     NSLog(@"latArray at index %i is %d in double", indexPath.row, [[latArray objectAtIndex:indexPath.row] doubleValue]); 
     NSLog(@"longArray at index %i is %d in double", indexPath.row, [[longArray objectAtIndex:indexPath.row] doubleValue]); 
     NSLog(@"latArray at index %i is %@", indexPath.row, [latArray objectAtIndex:indexPath.row]); 
     NSLog(@"longArray at index %i is %@", indexPath.row, [longArray objectAtIndex:indexPath.row]); 
     addressCoordinate.latitude = (CLLocationDegrees)[[latArray objectAtIndex:indexPath.row] doubleValue]; 
     addressCoordinate.longitude = (CLLocationDegrees)[[longArray objectAtIndex:indexPath.row] doubleValue]; 
     [mapView removeAnnotation:annotation]; 
     [annotation moveAnnotation:addressCoordinate]; 
     annotation.title = [titleArray objectAtIndex:indexPath.row]; 
     annotation.subtitle = [subtitleArray objectAtIndex:indexPath.row]; 
     [mapView addAnnotation:annotation]; 

    } 


// the console log 
2011-08-27 00:16:58.286 myApp[1140:207] latArray at index 0 is -1954958020 in double 
2011-08-27 00:16:58.287 myApp[1140:207] longArray at index 0 is 519365137 in double 
2011-08-27 00:16:58.287 myApp[1140:207] latArray at index 0 is 1.3450389335879 
2011-08-27 00:16:58.287 myApp[1140:207] longArray at index 0 is 103.69812749781 
2011-08-27 00:16:58.291 myApp[1140:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil' 

}

我如何準備的陣列

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    SBJsonParser *parser = [[SBJsonParser alloc] init]; 
    responseArray = [parser objectWithData:responseData]; 

    NSDictionary *dict = [responseArray objectAtIndex:i]; 
     for (int i = 1; i < [responseArray count]; i++) { 
       [latArray insertObject:[dict objectForKey:@"lat"] atIndex:i - 1]; 
     [longArray insertObject:[dict objectForKey:@"long"] atIndex:i - 1]; 
} 
+0

看起來你的註解對象最後是'nil'。你可以在你創建它的地方發佈代碼嗎? –

回答

1

什麼是latArray保存?我從你調用doubleValue的方式假設它的NSNumber。在這種情況下,你不需要轉換爲CLLocationDegrees在

addressCoordinate.latitude = (CLLocationDegrees)[[latArray objectAtIndex:indexPath.row] doubleValue]; 
addressCoordinate.longitude = (CLLocationDegrees)[[longArray objectAtIndex:indexPath.row] doubleValue]; 

,並使用

CLLocationCoordinate2DMake for CLLocationCoordinate2D 

,因爲它也增加一倍。但我認爲這個問題lia [mapView addAnnotation:annotation];我沒有看到這裏創建的任何註釋不知道它是不是伊娃。需要更多信息。

+0

哦,是的,我缺少註釋對象。 –

0

試試這個:

addressCoordinate = CLLocationCoordinate2DMake((CLLocationDegrees)[[latArray objectAtIndex:indexPath.row] doubleValue],(CLLocationDegrees)[[longArray objectAtIndex:indexPath.row] doubleValue]); 

,而不是試圖設置的緯度和經度,你現在看到什麼發生。

+0

它仍然是一樣的。 –