如何解析Objective C中Google Geo Coding API返回的结果 [英] How to parse the result returned by Google Geo Coding API in Objective C

查看:123
本文介绍了如何解析Objective C中Google Geo Coding API返回的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款需要经纬度的iPhone应用程序。我正在使用Google Map API进行地理编码,但我无法解析它返回的结果。结果是Json格式,我想要的结果是纬度和经度。
我试图查看之前的帖子和可用的API,但它没有成功。任何人都可以帮我吗?

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 ?

以下是Google请求返回的字符串。

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


推荐答案

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

这篇关于如何解析Objective C中Google Geo Coding API返回的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆