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?
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.
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);
}
}];
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"]);
}
Related
I need to add a new business place by using the google places API which is not already in google places database. For this we have used addPlace: callback:^ I believe this method once successfully added a new business location that call backs send a response to us and Places API are reviewed and, if approved, added to the global places database.
I have used the same call back method, but can't get any response from it.
My code is below,
GMSPlacesClient *placesClient;
[placesClient addPlace:userAddedPlace callback:^(GMSPlace *place, NSError *error) {
if (error != nil) {
NSLog(#"User Added Place error %#", [error localizedDescription]);
return;
}
NSLog(#"Added place with placeID %#", place.placeID);
NSLog(#"Added Place name %#", place.name);
NSLog(#"Added Place address %#", place.formattedAddress);
}];
We keep the location details in this object userAddedPlace
Is there any issue in this code? help me out why i didn't get the response after call this method. Thanks in advance.
Did you init placesClient object? try GMSPlacesClient *placesClient = [GMSPlacesClient sharedClient];
I saw this question but nobody answer how to change the language using the Google Places Api for iOS (GMSAutocompleteFilter). I need set by code the lenguaje parameter in order to Google api always return the results in english. I saw in the documentation that it's an optional parameter called 'language', but I cant find the way to set it in the iOS api (not as url example in the doc).
if(aQuery.length>0){
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterCity;
// filter.accessibilityLanguage = #"en";
[_placesClient autocompleteQuery:aQuery
bounds:nil
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(#"Autocomplete error %#", [error localizedDescription]);
handler(nil);
return;
}
if(results.count>0){
NSMutableArray *arrfinal=[NSMutableArray array];
for (GMSAutocompletePrediction* result in results) {
NSDictionary *aTempDict = [NSDictionary dictionaryWithObjectsAndKeys:result.attributedFullText.string,#"description",result.placeID,#"reference", nil];
PlaceObject *placeObj=[[PlaceObject alloc]initWithPlaceName:[aTempDict objectForKey:#"description"]];
placeObj.userInfo=aTempDict;
[arrfinal addObject:placeObj];
}
handler(arrfinal);
}else{
handler(nil);
}
}];
}else{
handler(nil);
}
You may need to use the Geocoding API to receive these responses in a different language. See here for the Reverse Geocoding article, with the language parameter a few paragraphs down.
You can make an NSURLRequest with the appropriate URL and language parameters. The response is in JSON format, so you should be able to handle this change dynamically within your code.
A cleaner way to do it might be to create a separate page that acts as a sort of web service for you. It accepts two parameters: A language code and an address. It loads the API using the language code requested, and reverse geocodes the address, providing the result. Your page would call this web service-like thing twice, once for each language, and then use the results as desired.
I am trying to display autocomplete results using Google Places API for iOS, I might be missing something but here is my code for the task:
- (void)placeAutocomplete {
GMSVisibleRegion visibleRegion = mapView_.projection.visibleRegion;
//GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:visibleRegion.farLeft
// coordinate:visibleRegion.nearRight];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterGeocode;
GMSPlacesClient *placesClient;
[placesClient autocompleteQuery:#"indira nagar"
bounds:nil
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(#"Autocomplete error %#", [error localizedDescription]);
return;
}
for (GMSAutocompletePrediction* result in results) {
NSLog(#"Result '%#' with placeID %#", result.attributedFullText.string, result.placeID);
}
}];
}
I've added the pods(pod 'GoogleMaps'), I even got the map But when i try autocomplete all I am getting is (in the log)"Google Maps SDK for iOS version: 1.10.17867.0" can any one help understand how it works?
This is the issue of my api key so i had to change(create a new one) to resolve this issue
Is there any way to get GSMAddress from placeID. I am interested in getting the address by using the placeID obtained from autocompleteQuery method.
P.S: I have a placeID and finding a way to get corresponding GMSAddress in iOS.
I found a thread here but that does not help.
Thanks,
After autocomplete, we do a place look up, then pass the coordinates to GMSGeocoder to retrieve the details, but I am still missing street_name and street_number.
CLLocationCoordinate2D addressCoordinates = CLLocationCoordinate2DMake(latitude,longitude);
GMSGeocoder* coder = [[GMSGeocoder alloc] init];
[coder reverseGeocodeCoordinate:addressCoordinates completionHandler:^(GMSReverseGeocodeResponse *results, NSError *error) {
if (error) {
NSLog(#"Error %#", error.description);
} else {
GMSAddress* address = [results firstResult];
NSLog(#"thoroughfare %#",address.thoroughfare);
NSLog(#"locality %#",address.locality);
NSLog(#"subLocality %#",address.subLocality);
NSLog(#"administrativeArea %#",address.administrativeArea);
NSLog(#"postalCode %#",address.postalCode);
NSLog(#"country %#",address.country);
NSLog(#"lines %#",address.lines);
}
}];
I'm thinking of switching to iOS's own reverse geocoder.
[CLGeocoder reverseGeocodeLocation:[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] completionHandler:
^(NSArray* placemarks, NSError* error) {
It seems there is no direct way to get the GSMAddress from GMSPlace placeID.
One way that I figured out is to use GMSGeocoder().reverseGeocodeCoordinate method along with coordinate property from GMSPlace.
The steps I followed can be summed as:
Use lookUpPlaceID method to find the coordinates
Reverse geocode using the coordinates obtained from lookUpPlaceID
Code is as follows:
placesClient?.lookUpPlaceID(placeID, callback: {
(place, error) -> Void in
// Get place.coordinate
GMSGeocoder().reverseGeocodeCoordinate(place!.coordinate, completionHandler: {
(response, error) -> Void in
for addObj in response.results() {
// Address object
}
})
})
Comments and better solutions are welcome.
I am working on Google Places AutoComplete Service where i need to filter the places specific to country and found this.
It is working good untill when i pass a parameter components=country:se to filter the autocomplete field its response is REQUEST_DENIED
NSMutableString *url = [NSMutableString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%#&components=country:se&sensor=%#&key=%#",
[input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
SPBooleanStringForBool(sensor), key];
I need the autocomplete to give suggestion specific to country.
Thanks
This works fine for me.
You can set this by adding the bounds for the region by adding its extreme top left and bottom right location points of the the Geographical area.
like for USA i used.
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:CLLocationCoordinate2DMake(52.00, 148.00)
coordinate:CLLocationCoordinate2DMake(18.00, -71.00)];
GMSPlacesClient * placesClient = [[GMSPlacesClient alloc] init];
[placesClient autocompleteQuery:fieldValue bounds:bounds filter:nil
callback:^(NSArray *results, NSError *error)
{
if (error != nil)
{
NSLog(#"Autocomplete error %#", [error localizedDescription]);
return;
}
else
{
[self.autoComplete removeAllObjects];
[self.AddressPlaceID removeAllObjects];
for (GMSAutocompletePrediction* result in results)
{
CustomAddress * parseAddress = [[CustomAddress alloc] init];
NSString * adString = [parseAddress removeCountryFromAddress:result.attributedFullText.string];
[self.autoComplete addObject:adString];
[self.AddressPlaceID addObject:result.placeID];
}
[self.autoCompleteTable reloadData];
}
}];
But you can still get the other location results . It will prefer first the geographical bounds you define in the GMCoordinateBounds
Why do not use the iOS SDK to obtain country specific results? Follow this answer.