Gavin
Gavin

Reputation: 2824

Convert NSArray to CLLCoordinate

I am trying to covert an array of Latitude and Longitude into coordinate and set annotation to it. The app crashes upon doing so. Where am I doing it wrong? Somehow the data got messed up when it changes to double.

- (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'

}

How I prepared the arrays

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

Upvotes: 1

Views: 462

Answers (2)

sarunw
sarunw

Reputation: 8176

What is latArray keep ? I assume its NSNumber from the way you call doubleValue. In that case you don't need to cast to CLLocationDegrees in

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

and use

CLLocationCoordinate2DMake for CLLocationCoordinate2D

because its also double. But I think the problem lia in [mapView addAnnotation:annotation]; I don't see any annotation created here don't know if it is ivar. Need more info.

Upvotes: 1

James
James

Reputation: 2282

Try this:

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

instead of trying to set the latitude and longitude as you are now and see what happens.

Upvotes: 0

Related Questions