can I change the result language of Google Places API for iOS?

纵饮孤独 提交于 2019-11-28 14:28:39

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.

Chiman Song

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

  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

}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!