I am using Google Places Api for iOS(GMSAutocompleteFilter) to search nearby places,
if I set English(US) as my system language, it will return Traditional Chinese results(which is acceptable since I am in taiwan).
but if I set Traditional Chinese as my system language, the api will return simplified Chinese results(what is the logic behind?)
How can I change the language of api search results?
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
// filter.type = kGMSPlacesAutocompleteTypeFilterCity;
[placesClient autocompleteQuery:searchText
bounds:nil
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(@"Autocomplete error %@", [error localizedDescription]);
return;
}
self.placeResults = results;
[self.resultsList reloadData];
for (GMSAutocompletePrediction* result in results) {
NSLog(@"Result '%@' with placeID %@", result.attributedFullText.string, result.placeID);
}
}];
I'm an engineer on the Places API for iOS. Thanks for posting about this! This definitely looks like incorrect behaviour—we should always use the system language for autocomplete results.
Would you mind filing a request on the issue tracker? This will help us prioritize against other issues and help you to track the status of the bug.
you can always change the language setting programmatically.
Swift 4
UserDefaults.standard.set(["en_US"], forKey: "AppleLanguages")
I've found a workaround for this issue.
Since Google Place iOS SDK doesn't allow us to specify the language used (which is very weird indeed), we'll have to look up a place with the Google Place API (Web-based).
Install the swift wrapper API for the Google Place API
https://github.com/honghaoz/Swift-Google-Maps-API
When you get the place with its placeID
, use it to look up in the language you want.
The following is the code snippet from my GMSAutocompleteResultsViewControllerDelegate
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
Log.debug("Place name: \(String(describing: place.name))")
Log.debug("Place address: \(String(describing: place.formattedAddress))")
Log.debug("Place attributions: \(String(describing: place.attributions))")
guard let placeID = place.placeID else {
return
}
/// Use Google Place API to lookup place information in English
/// https://developers.google.com/places/web-service/intro
GooglePlacesAPI.GooglePlaces.placeDetails(forPlaceID: placeID, language: "en") { (response, error) -> Void in
guard let place = response?.result, response?.status == GooglePlaces.StatusCode.ok else {
Log.debug("Error = \(String(describing: response?.errorMessage))")
return
}
let postalCode = place.addressComponents.filter({ (components) -> Bool in
return components.types.contains(kGMSPlaceTypePostalCode)
}).first?.longName
/// Save Selection
let location = Location(name: place.name ?? MessageSource.getString("landmark.unknown_loc"), addr: place.formattedAddress, postal: postalCode)
self.currentLocation = location
self.searchController?.isActive = false
///Set text in search bar
self.searchController?.searchBar.text = place.name
}
}