如何从PlaceID或GMSPlace获取GMSAddress

7

我正在使用Google Places API for iOS的自动完成功能,它返回placeID。如何从placeID获取GMSAddress?我目前使用lookupPlaceID来获取GMSPlace,然后使用GMSPlace中坐标的reverseGeocodeCoordinate来获取GMSAddress。但它需要三次请求谷歌API:自动完成、lookupPlaceID、reverseGeocodeCoordinate。

有没有办法直接从PlaceID或GMSPlace获取GMSAddress?


1
根据GMSAddress文档,它表示从reverse geocode request中得到的结果,包含一个易读的地址。因此,您必须执行reverseGeocodeCoordinate以获取GMSAddress。因此,您无法避免使用AutoCompletelookupPlacesID,然后再使用reverseGeocoderCoordinate - ztan
@ztan 已经明白了。谢谢。 - JordanChina
这个解决方案可能会遇到一个问题,即使 coordinate 完全相同,GMSPlacename 属性可能与 GMSAddressthoroughfare 属性不同。 - George Marmaridis
3个回答

5

这是Google Maps SDK for iOS 1.1.0版本的一个bug。请参见这里

正如文档这里所述,GMSPlace实例上的address属性应该公开一个GMSAddress

我刚刚转而使用REST API。编写API的快速包装器并完全放弃Google SDK并不太困难。如果您正在使用Alamofire,请联系我,我可以分享我所写的内容。


你是对的。在文档中,有地址属性,但实际上在SDK中没有返回。 - JordanChina
2
我真的希望Google能在1.10.2中添加那个修复... 获取地点详情却无法访问地址... 希望下一个版本很快发布。 - manonthemoon
1
请大家为该问题“点赞”,以便它能够得到更多来自谷歌的关注。https://code.google.com/p/gmaps-api-issues/issues/detail?id=8121 - Robert
目前使用的 Google Maps SDK for iOS version: 1.10.19729.0 仍然存在问题。甚至在文档中已经移除了 address 属性。虽然 GMSPlace 中有一个 addressComponents 属性,但它并没有被公开访问,所以你只能通过 [place valueForKey:@"addressComponents"] 来访问,这样非常不安全!请参见我的评论:https://code.google.com/p/gmaps-api-issues/issues/detail?id=8121#c8。希望尽快解决! - damirstuhec
我是那个写了那个缺陷报告的人,问题仍然存在。我一直在使用REST API来获取placeId的JSON表示形式,然后使用它来创建iOS对象。https://maps.googleapis.com/maps/api/place/details/json?placeid='id' - chrismanderson

3

我可以通过一次API调用从placedID获取地址,以下是代码:

GMSPlacesClient *placesClient = [[GMSPlacesClient alloc] init];

[placesClient lookUpPlaceID:result.placeID callback:^(GMSPlace *place, NSError *error) {
    if (error != nil) {
        NSLog(@"Place Details error %@", [error localizedDescription]);
        return;
    }
    if (place != nil) {
        NSString *locality = @"";
        for (GMSAddressComponent *add in place.addressComponents) {
            //locality
              NSLog(@"%@",add.type);
              NSLog(@"%@",add.name);
//type will be street_name, locality, admin_area, country,
//name will hold the corresponding value   
        }

    } else {
        NSLog(@"No place details for %@", result.placeID);
    }

}];

1

现在,对于此版本的SDK(1.10.5),下面的代码是否足够安全?在接下来的6个月中,这个代码块出现错误的概率是多少?

NSArray* dic = [place valueForKey:@"addressComponents"];

for (int i=0;i<[dic count];i++) {
    if ([[[dic objectAtIndex:i] valueForKey:@"type"] isEqualToString:@"street_number"]) {
    NSLog(@"street_number: %@",[[dic objectAtIndex:i] valueForKey:@"name"]);
}

如果您确保从不意外更新SDK(例如在没有明确定义的SDK版本的情况下运行pod install),那么是的,在避免崩溃方面这是安全的。但是,您无法确定addressComponents包含什么内容,可能最终得不到正确的数据。我目前遇到了同样的问题,并更喜欢问题描述中所述的方法。 - George Marmaridis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接