Google Places iOS: How to provide context to API for relevant local results

∥☆過路亽.° 提交于 2019-12-11 05:57:52

问题


Using Google Places API for iOS, I am using the Place AutoComplete feature. When I start typing a place name for example 'Starbucks' it gives me starbucks location in several countries but not my local starbucks. How do I pass the current location to the API so the search results start from closest to my location?


回答1:


You can pass a GMSCoordinateBounds to the which is "object biasing the results to a specific area specified by latitude and longitude bounds".

They have an example on their site showing how to do this:

let visibleRegion = self.mapView.projection.visibleRegion()
let bounds = GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight)
let filter = GMSAutocompleteFilter()
filter.type = GMSPlacesAutocompleteTypeFilter.City
placesClient.autocompleteQuery("Sydney Oper", bounds: bounds, filter: filter, callback: { (results, error: NSError?) -> Void in
    guard error == nil else {
        print("Autocomplete error \(error)")
        return
    }

    for result in results! {
        print("Result \(result.attributedFullText) with placeID \(result.placeID)")
    }
})

This example show how to pull this data from a map view but you can also create the GMSCoordinateBounds manually.

Hope this helps.



来源:https://stackoverflow.com/questions/40513803/google-places-ios-how-to-provide-context-to-api-for-relevant-local-results

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