-1

I have the following NSArray:

(
        {
        geometry =         {
            location =             {
                lat = "37.789632";
                lng = "-122.417004";
            };
        };
        icon = "http://maps.gstatic.com/mapfiles/place_api/icons/doctor-71.png";
  vicinity = "900 Hyde Street, San Francisco";
    },
        {
        geometry =         {
            location =             {
                lat = "37.794774";
                lng = "-122.398941";
            };
        };
        icon = "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png";
 vicinity = "2 Embarcadero Center, San Francisco";
}
)

From this how can I get the latitude and longitude from each index?

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Honey
  • 2,840
  • 11
  • 37
  • 71

2 Answers2

7

You didn't even try to think this one out?? I'll help you out for the funs, but help yourself man

NSArray *values = ...;
for (NSDictionary *geometry in values) {
    NSDictionary *loc = [geometry valueForKey:@"location"];
    if (loc) {
        NSNumber *lat = [loc valueForKey:@"lat"];
        NSNumber *lng = [loc valueForKey:@"lng"];
        if (lat && lng) {
            // do whatever
        }
    }
}

EDIT: maybe it's like this

NSArray *values = ...;
for (NSDictionary *item in values) {
    NSDictionary *geo = [item valueForKey:@"geometry"];
    if (geo) {
        NSDictionary *loc = [geometry valueForKey:@"location"];
        if (loc) {
            NSNumber *lat = [loc valueForKey:@"lat"];
            NSNumber *lng = [loc valueForKey:@"lng"];
            if (lat && lng) {
                // do whatever
            }
        }
    }
}
Ismael
  • 3,927
  • 3
  • 15
  • 23
  • 1
    @arizah Why don't you take this answer as a starting point and try to get it working - that's what programming is about. You can't expect to just code by copying and pasting chunks of code. We are here to help you, NOT to do your work for you. – Nick Bull Dec 24 '12 at 12:09
  • I recommend u to first give correct answer and then downvote others questions. – Honey Dec 24 '12 at 12:10
  • 1
    I'm sorry for not being able to decypher your data structure in one attempt. Maybe next time you can work it out on your own and not depend on others giving you inaccurate responses :) – Ismael Dec 24 '12 at 12:17
2
for (int i=0 ; i<[parsedAry count] ; i++) {

    NSMutableDictionary * location = [[[parsedAry objectAtIndex:i]objectForKey:@"geometry"]objectForKey:@"location"];
    NSString * lat = [location objectForKey:"lat"];
    NSString * lng = [location objectForKey:"lng"]; 
    [latAry addObject:lat];
    [LongAry addObject:lng];


}

You have to create and allocate latAry and longAry

thavasidurai
  • 1,972
  • 1
  • 26
  • 51