3

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

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Goutham
  • 51
  • 5
  • Are you sure that the key is correct? Why are you using NSMutableString and not NSString? Are you adding more things to the string afterwards? Can you debug or NSLog the NSURL just before the request is done to check that it contains exactly what you expect? BTW, are you using a Server Key that you aquired from Google? – Hunkpapa Dec 05 '13 at 10:47
  • Yes i used the server key which is generated from Google API Console. Yes i am appending some parameters to the URL https://github.com/spoletto/SPGooglePlacesAutocomplete/blob/master/SPGooglePlacesAutocomplete/SPGooglePlacesAutocompleteQuery.m. And this url i am getting in the Log "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=ku&types=geocode&components=country:se&sensor=false&key=AIzaSyCmnaKAOGprFYegMGC_B0J10f1VU939C-M" – Goutham Dec 05 '13 at 11:17
  • There is a doublequote char at the end of the link. When I removed that, the call works when I run it in my web browser. – Hunkpapa Dec 05 '13 at 11:31
  • Yes @Hunkpapa i have done the same thing i have tested it on browser it worked so as i done the same thing like below. Thanxs NSMutableString *url = [NSMutableString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=geocode&components=country:ind&sensor=%@&key=AIzaSyCjtaR4L4qXW238MLxaOZwtrEtLERf4_Zg", [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], SPBooleanStringForBool(sensor)]; – Goutham Dec 05 '13 at 12:59
  • Ok, but where does the extra doublequote come from? It must be added somewhere. Add an NSLog statement just before returning from the method googleURLString, so you can see exactly what the url contains – Hunkpapa Dec 05 '13 at 13:43
  • Try to Customize this one : https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField – Mrug Jun 10 '15 at 11:38

2 Answers2

1

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

Sucharu Hasija
  • 1,096
  • 11
  • 23
0

Why do not use the iOS SDK to obtain country specific results? Follow this answer.

Community
  • 1
  • 1
Ankahathara
  • 2,866
  • 2
  • 20
  • 26