开发者

How to parse the result returned by Google Geo Coding API in Objective C

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"
}


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! ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜