0

I've been trying to search an address on google and return the Latitude and Longitude of that address. I'm almost there =D

The problem is.. I get a giant json back and I'm not sure how to get only the Latitude and Longitude.

I'm using AFNetworking.

//Build the string to Query Google Maps.
        NSMutableString *urlString = [NSMutableString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false", address];

        //Replace Spaces with a '+' character.
        [urlString setString:[urlString stringByReplacingOccurrencesOfString:@" " withString:@"+"]];

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            //NSLog(@"minha location %@", JSON);
            //NSLog(@"minha url %@", urlString);

            NSArray *resul = [[NSArray alloc] init];
            resultado = [JSON objectForKey:@"results"];
            NSLog(@"Resultado: %@", result);

Using this I get this:

2013-04-19 08:04:57.092 XePop[7011:907] Result: (
        {
        "address_components" =         (
                        {
                "long_name" = 379;
                "short_name" = 379;
                types =                 (
                    "street_number"
                );
            },
                        {
                "long_name" = "Rua Domingos Ol\U00edmpio";
                "short_name" = "R. Domingos Ol\U00edmpio";
                types =                 (
                    route
                );
            },
                        {
                "long_name" = Centro;
                "short_name" = Centro;
                types =                 (
                    sublocality,
                    political
                );
            },
                        {
                "long_name" = Sobral;
                "short_name" = Sobral;
                types =                 (
                    locality,
                    political
                );
            },
                        {
                "long_name" = "Cear\U00e1";
                "short_name" = CE;
                types =                 (
                    "administrative_area_level_1",
                    political
                );
            },
                        {
                "long_name" = "Rep\U00fablica Federativa do Brasil";
                "short_name" = BR;
                types =                 (
                    country,
                    political
                );
            },
                        {
                "long_name" = "62011-140";
                "short_name" = "62011-140";
                types =                 (
                    "postal_code"
                );
            }
        );
        "formatted_address" = "Rua Domingos Ol\U00edmpio, 379 - Centro, Sobral - CE, 62011-140, Rep\U00fablica Federativa do Brasil";
        geometry =         {
            bounds =             {
                northeast =                 {
                    lat = "-3.6878671";
                    lng = "-40.35096009999999";
                };
                southwest =                 {
                    lat = "-3.6878797";
                    lng = "-40.35097349999999";
                };
            };
            location =             {
                lat = "-3.6878797";
                lng = "-40.35097349999999";
            };
            "location_type" = "RANGE_INTERPOLATED";
            viewport =             {
                northeast =                 {
                    lat = "-3.686524419708497";
                    lng = "-40.3496178197085";
                };
                southwest =                 {
                    lat = "-3.689222380291502";
                    lng = "-40.35231578029149";
                };
            };
        };
        types =         (
            "street_address"
        );
    }
)

How Can I get only the Location Part, which has:

location =             {
                lat = "-3.6878797";
                lng = "-40.35097349999999";
            };

I've tried [JSON objectForKey:@"location"], but it returns null.

Please help-me out.

Thank you for your consideration.

César Martins
  • 95
  • 1
  • 1
  • 6

1 Answers1

3

The desired field is available at:

[JSON valueForKeyPath:@"geometry.location"];

mattt
  • 19,544
  • 7
  • 73
  • 84