can I change the result language of Google Places

2020-02-07 06:24发布

问题:

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

回答1:

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.



回答2:

you can always change the language setting programmatically. Swift 4

UserDefaults.standard.set(["en_US"], forKey: "AppleLanguages")


回答3:

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).

  1. Install the swift wrapper API for the Google Place API

    https://github.com/honghaoz/Swift-Google-Maps-API

  2. 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

    }
}