Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get GMSAddress from PlaceID or GMSPlace

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?

like image 578
JordanChina Avatar asked Jun 29 '15 01:06

JordanChina


3 Answers

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.

like image 166
Keith Norman Avatar answered Nov 11 '22 02:11

Keith Norman


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

}];
like image 35
sahiljain Avatar answered Nov 11 '22 03:11

sahiljain


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"]);
}
like image 43
bendigi Avatar answered Nov 11 '22 01:11

bendigi