0

I am working on one iPhone app which needs latitude and longitude from address. I am using Google Map API for Geo coding but I could not able to parse the result returned by it. Result is in Json format and all I want form that result is Latitude and Longitude. I tried to look over earlier posts and available API but it did not worked out. Can any one help me out here ?

Following is the string returned by the Google Request.

  "results" : [
  {
     "address_components" : [
        {
           "long_name" : "15220",
           "short_name" : "15220",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "N Western Ave",
           "short_name" : "N Western Ave",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Edmond",
           "short_name" : "Edmond",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Oklahoma City",
           "short_name" : "Oklahoma City",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Oklahoma",
           "short_name" : "Oklahoma",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Oklahoma",
           "short_name" : "OK",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "73013",
           "short_name" : "73013",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "15220 N Western Ave, Edmond, OK 73013, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 35.6246310,
              "lng" : -97.53124129999999
           },
           "southwest" : {
              "lat" : 35.62462730,
              "lng" : -97.53126360
           }
        },
        "location" : {
           "lat" : 35.6246310,
           "lng" : -97.53124129999999
        },
        "location_type" : "RANGE_INTERPOLATED",
        "viewport" : {
           "northeast" : {
              "lat" : 35.62597813029150,
              "lng" : -97.52990346970849
           },
           "southwest" : {
              "lat" : 35.62328016970850,
              "lng" : -97.53260143029149
           }
        }
     },
     "types" : [ "street_address" ]
  }
],
"status" : "OK"
}
slonkar
  • 4,055
  • 8
  • 39
  • 63

1 Answers1

2

Use SBJson:

NSDictionary *partialJsonDict = [[[yourIncomingJsonAsAString JSONValue] objectForKey:@"results"]] objectAtIndex:0];
NSDictionary *geometryDict = [partialJsonDict objectForKey:@"geometry"];
Float32 latitude = [[[geometryDict objectForKey:@"location"] objectForKey:@"lat"] floatValue];
Float32 latitude = [[[geometryDict objectForKey:@"location"] objectForKey:@"lng"] floatValue];

Yes, it really is just that easy! ;)

prasphy
  • 45
  • 1
  • 9
GarlicFries
  • 8,095
  • 5
  • 36
  • 53
  • I came across that library but I am really new to JSON and iPhone development. I would really appreciate if your give me some more on that. Thanks. – slonkar Aug 08 '11 at 18:23
  • Thanks. And besides that I should only import SBJson.h file to my project. right ? – slonkar Aug 08 '11 at 18:26
  • Whoops...didn't see that that top level node held an array...updated the code. – GarlicFries Aug 08 '11 at 18:28
  • If you scroll down on that Github page, it tells you how to use it. But yes, all you need to do is import that header file wherever you want to use SBJson. – thomashw Aug 08 '11 at 18:30
  • You'll need more than just `SBJson.h`. Follow the installation directions [here](https://github.com/stig/json-framework/). – GarlicFries Aug 08 '11 at 18:30