7

I am using Autocomplete of Google Places API for iOS, and it returns placeID. How can I get GMSAddress from the placeID? I currently use lookupPlaceID to get GMSPlace, and then use reverseGeocodeCoordinate to get GMSAddress from coordinate in GMSPlace. But it request Google API 3 times: AutoComplete, lookupPlaceID, reverseGeocodeCoordinate.

Is there any way to get GMSAddress from PlaceID or GMSPlace directly?

ztan
  • 6,861
  • 2
  • 24
  • 44
JordanChina
  • 335
  • 3
  • 13
  • 1
    From the [GMSAddress](https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_address) documentation, it says `GMSAddress` is a result from a `reverse geocode request`, containing a human-readable address. So you have to do a `reverseGeocodeCoordinate` to get the `GMSAddress`. So you cant avoid using `AutoComplete` and `lookupPlacesID` then `reverseGeocoderCoordinate`. – ztan Jun 29 '15 at 16:44
  • @ztan got it. thanks – JordanChina Jun 30 '15 at 01:15
  • A problem you might have with this solution is that `GMSPlace`'s `name` property might be different from `GMSAddress`'s `thoroughfare` property, even if the `coordinate` is exactly the same. – George Marmaridis Nov 27 '15 at 10:57

3 Answers3

5

This is a bug with 1.1.0 of the Google Maps SDK for iOS. See here.

As the documentation states here, the address property should expose a GMSAddress on GMSPlace instances.

I've just switched to using the REST API. It's not too bad to write a quick wrapper for the API and ditch the Google SDK completely. If you are using Alamofire ping me and I may be able to share what I've written.

Keith Norman
  • 505
  • 4
  • 11
  • you are right. In the document, there is address property, but actually not returned in SDK. – JordanChina Jul 15 '15 at 02:51
  • 2
    I really hope that Google will add that fix in 1.10.2 ... Fetching the place details without being able to access to the address... Hopefully, the next version will be released soon. – manonthemoon Jul 19 '15 at 22:48
  • 1
    please can everyone 'star' the issue so that it receives more attention form google. https://code.google.com/p/gmaps-api-issues/issues/detail?id=8121 – Robert Jul 21 '15 at 11:59
  • Still a problem today with `Google Maps SDK for iOS version: 1.10.19729.0`. They have even removed `address` property in documentation. Although, there is a `addressComponents` property on `GMSPlace` but is not exposed, so you can access it only through `[place valueForKey:@"addressComponents"]` which is not safe at all! See my comment here: https://code.google.com/p/gmaps-api-issues/issues/detail?id=8121#c8. Hope this gets fixed soon! – damirstuhec Aug 27 '15 at 05:37
  • I was the one who wrote that bug report, and it's still an issue. I've been using the REST API to fetch the JSON representation for a placeId, and then using that to create iOS objects. `https://maps.googleapis.com/maps/api/place/details/json?placeid='id'` – chrismanderson Nov 24 '15 at 19:09
3

I could get the address from placedID in one API call, here is the code:

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

}];
sahiljain
  • 2,215
  • 1
  • 29
  • 39
1

Now for the time being, is the code below safe (enough) for this version of the SDK (1.10.5)? What is the probability that this will break in the next 6 months?

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"]);
}
bendigi
  • 590
  • 6
  • 15
  • If you make sure you never accidentally update the SDK (e.g. running a `pod install` without an explicitly defined SDK version), then yes, this is safe in terms of not crashing. However, you cannot be sure of what `addressComponents` contains and you may end up without proper data. I have currently run into the same issue and preferred the approach described in the question. – George Marmaridis Nov 27 '15 at 16:36