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

亡梦爱人 提交于 2019-12-06 15:24:39

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/6986616/how-to-parse-the-result-returned-by-google-geo-coding-api-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!