ios - 在 IOS 中从 google map place api 中提取需要信息

标签 ios objective-c json google-maps-api-3 gps

我正在开发 GPS 应用程序,我需要根据某个地方的经度和纬度来确定位置。我能够确定经度和纬度以及位置,但我得到的是 google maps api 调用的结果json 格式,其中有很多数据以及城市名称。

我这样做了。(我正在使用模拟器,所以我已经通过了固定的纬度和经度)

NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297, 76.444017];
    NSError* error;
        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
        locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];

    NSLog(@"%@",locationString ); 

我得到的输出字符串是

015-04-16 10:38:45.056 demolocationapp[690:12005] {

   results : [
      {
         address_components : [
            {
               long_name : Rauni Kalan Link Road,
               short_name : Rauni Kalan Link Rd,
               types : [ route ]
            },
            {
             **long_name : Rauni Kalan,
               short_name : Rauni Kalan,
               types : [ locality, political]**

            },
            {
               long_name : Rupnagar,
               short_name : Rupnagar,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            },
            {
               long_name : 140102,
               short_name : 140102,
               types : [ postal_code ]
            }
         ],
         formatted_address : Rauni Kalan Link Road, Rauni Kalan, Punjab 140102, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.8209903,
                  lng : 76.4550081
               },
               southwest : {
                  lat : 30.8178417,
                  lng : 76.44540219999999
               }
            },
            location : {
               lat : 30.8186057,
               lng : 76.4507395
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.8209903,
                  lng : 76.4550081
               },
               southwest : {
                  lat : 30.8178417,
                  lng : 76.44540219999999
               }
            }
         },
         place_id : ChIJc8IoHwICEDkR38Ie5TOeGu0,
         types : [ route ]
      },
      {
         address_components : [
            {
               long_name : Rauni Kalan,
               short_name : Rauni Kalan,
               types : [ locality, political ]
            },
            {
               long_name : Rupnagar,
               short_name : Rupnagar,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            },
            {
               long_name : 140102,
               short_name : 140102,
               types : [ postal_code ]
            }
         ],
         formatted_address : Rauni Kalan, Punjab 140102, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.8219531,
                  lng : 76.4462614
               },
               southwest : {
                  lat : 30.81705139999999,
                  lng : 76.4414979
               }
            },
            location : {
               lat : 30.8192965,
               lng : 76.44401660000001
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.8219531,
                  lng : 76.4462614
               },
               southwest : {
                  lat : 30.81705139999999,
                  lng : 76.4414979
               }
            }
         },
         place_id : ChIJ_f1PWh0CEDkRKlzFwhc62zo,
         types : [ locality, political ]
      },
      {
         address_components : [
            {
               long_name : 140102,
               short_name : 140102,
               types : [ postal_code ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : Punjab 140102, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.869706,
                  lng : 76.47508049999999
               },
               southwest : {
                  lat : 30.7970006,
                  lng : 76.3335855
               }
            },
            location : {
               lat : 30.8288892,
               lng : 76.4215988
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.869706,
                  lng : 76.47508049999999
               },
               southwest : {
                  lat : 30.7970006,
                  lng : 76.3335855
               }
            }
         },
         place_id : ChIJHX2_rA0BEDkR_QslHcc_M0g,
         types : [ postal_code ]
      },
      {
         address_components : [
            {
               long_name : Fatehgarh Sahib,
               short_name : Fatehgarh Sahib,
               types : [ administrative_area_level_2, political ]
            },
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : Fatehgarh Sahib, Punjab, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 30.8779699,
                  lng : 76.63427999999999
               },
               southwest : {
                  lat : 30.4275501,
                  lng : 76.0761799
               }
            },
            location : {
               lat : 30.6810924,
               lng : 76.4100267
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 30.8779699,
                  lng : 76.63427999999999
               },
               southwest : {
                  lat : 30.4275501,
                  lng : 76.0761799
               }
            }
         },
         place_id : ChIJqf6CHWocEDkR4EljDGd5be8,
         types : [ administrative_area_level_2, political ]
      },
      {
         address_components : [
            {
               long_name : Punjab,
               short_name : PB,
               types : [ administrative_area_level_1, political ]
            },
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : Punjab, India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 32.4981352,
                  lng : 76.92175809999999
               },
               southwest : {
                  lat : 29.537147,
                  lng : 73.8708879
               }
            },
            location : {
               lat : 31.1471305,
               lng : 75.34121789999999
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 32.4981352,
                  lng : 76.92175809999999
               },
               southwest : {
                  lat : 29.537147,
                  lng : 73.8708879
               }
            }
         },
         place_id : ChIJVXOeVqpkGTkRYYijAzEmvY8,
         types : [ administrative_area_level_1, political ]
      },
      {
         address_components : [
            {
               long_name : India,
               short_name : IN,
               types : [ country, political ]
            }
         ],
         formatted_address : India,
         geometry : {
            bounds : {
               northeast : {
                  lat : 35.5087008,
                  lng : 97.395561
               },
               southwest : {
                  lat : 6.7535159,
                  lng : 68.162386
               }
            },
            location : {
               lat : 20.593684,
               lng : 78.96288
            },
            location_type : APPROXIMATE,
            viewport : {
               northeast : {
                  lat : 35.5087008,
                  lng : 97.395561
               },
               southwest : {
                  lat : 6.7535159,
                  lng : 68.16288519999999
               }
            }
         },
         place_id : ChIJkbeSa_BfYzARphNChaFPjNc,
         types : [ country, political ]
      }
   ],
   status : OK
}

我已将我的必需数据放在 type 是 locality,political 的地方,在 **Required Text **

我怎样才能得到它

最佳答案

用过这个方法

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",30.819297, 76.444017]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (!error)
    {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

             NSArray *arr=[[json valueForKeyPath:@"results.address_components"] objectAtIndex:0];

        for (NSDictionary *dictAddress in arr)
        {
            if ([[dictAddress objectForKey:@"types"] count] > 0)
            {
                if([[[dictAddress objectForKey:@"types"] objectAtIndex:0] isEqualToString:@"locality"])
                {
                    return [dictAddress objectForKey:@"long_name"];
                }
                else if([[[dictAddress objectForKey:@"types"] objectAtIndex:0] isEqualToString:@"political"])
                {
                    return [dictAddress objectForKey:@"long_name"];
                }
            }
            else
            {
                return @"";
            }
        }
        return @"";
        }
    }
}];

使用此方法,您将找到类型以“locality”或“political”开头的区域名称。

但是我已经获取了 api 响应的第一个对象,但是如果您需要的不是这个,那么请循环获取调用的 json 对象。

希望对您有所帮助。

关于ios - 在 IOS 中从 google map place api 中提取需要信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665907/

相关文章:

ios - 应用因文件选择而被拒绝

ios - 我们需要在 ARC 的 UIAnimationBlocks 中使用 __weak self 吗?

objective-c - NSMutableArray 被释放了吗?

objective-c - 使用 Cocoa 将 OS X 中的音频录制为 FLAC

json - 过滤存储在 PostgreSQL 表中的 JSON 内部键

iOS:Tabbar - 加载选项卡的默认状态

ios - Sprite Kit中如何实现鼠标关节?

ios - 发现多个名为 'tag' 的方法具有不匹配的结果、参数类型或属性

python - 将特定的 pydantic 对象字段设置为 null 时不序列化

javascript - 有没有办法通过多个数组来增加 JSON 数组键值,而不是从每个数组开始?