Search code examples
iphoneobjective-ciosmkmapviewcllocation

How to seperate Latitude and Longitude Values From the JSON Output?


I am trying two draw route between two location,For that I Fetch all the points from Google Map API Web Service.(JSON Output Format). After parsing JSON Data and De cording points i stored all points on NSMutableArray. Each array of index contains this type of values.

"<+10.90180969, +76.19167328> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/04/12 10:18:10 AM India Standard Time",

Now i want to separate latitude and longitude values.

latitude  : +10.90180969
longitude : +76.19167328

How to get this values from each index of array?


Solution

  • Here is something in a very crude form and I guess its a brute solution

    NSString* str =     @"<+10.90180969, +76.19167328> +/- 0.00m (speed -1.00 mps  course -1.00) @ 12/04/12 10:18:10 AM India Standard Time";
    NSArray* arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    
    if ([arr count] > 1) {
       NSString* coordinateStr =  [arr objectAtIndex:1];        
       NSArray* arrCoordinate = [coordinateStr componentsSeparatedByString:@","];        
        NSLog(@"%@",arrCoordinate);
    }
    

    Looking at the string it shows that you are printing the description of a CLLocation object "someObject" , You can also access the latitude and longitude such as

       CLLocationCoordinate2D location = [someObject coordinate];
        NSLog(@" \nLatitude: %.6f \nLongitude: %.6f",location.latitude,location.longitude);
    

    OutPut will be : Latitude: 28.621873 Longitude: 77.388897

    but this won't give you the signs i.e "+" , or "-"

    Hope it helps.