Search code examples
iosnsmutablearraycoordinatescllocationvalueconverter

How to convert NSMutableArray to CLLocationCoordinate2D


I have NSMutableArray:

        userCoordinates = [[d objectForKey:@"geo"] objectForKey:@"coordinates"];

        NSLog(@"%@",userCoordinates);

NSLog shows:

(
    "19.365367",
    "-99.159887"
)

Then I need to convert this array to CllocationCoordinate2D to use it for create annotation. Sorry my english. Thanks.


Solution

  • It seems that your array contains two NSString objects - to convert them to the appropriate floating-point numbers, use the doubleValue method of NSString:

    double lat = [(NSString *)[userCoordinates objectAtIndex:0] doubleValue];
    double lon = [(NSString *)[userCoordinates objectAtIndex:1] doubleValue];
    
    CLLocationCoordinate2D coords = (CLLocationCoordinate2D){ lat, lon };