将NSArray转换为CLLCoordinate

时间:2011-08-26 16:22:40

标签: iphone nsarray cllocationmanager

我试图将纬度和经度数组转换为坐标并为其设置注释。应用程序在这样做时崩溃了。我在哪里做错了?不知何故,当数据变为双倍时,数据变得混乱。

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

2 个答案:

答案 0 :(得分:1)

latArray保留什么?我从你调用doubleValue的方式中假设它的NSNumber。在这种情况下,您不需要在

中强制转换为CLLocationDegrees
addressCoordinate.latitude =  (CLLocationDegrees)[[latArray objectAtIndex:indexPath.row] doubleValue];
addressCoordinate.longitude =  (CLLocationDegrees)[[longArray objectAtIndex:indexPath.row] doubleValue];

并使用

CLLocationCoordinate2DMake for CLLocationCoordinate2D

因为它也是双倍的。但我认为[mapView addAnnotation:annotation];中的问题lia我没有看到这里创建的任何注释都不知道它是否是ivar。需要更多信息。

答案 1 :(得分:0)

试试这个:

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

而不是像现在一样尝试设置纬度和经度,看看会发生什么。