Google Places iOS: How to provide context to API f

2019-07-28 18:37发布

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条回答
神经病院院长
2楼-- · 2019-07-28 19:28

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.

查看更多
登录 后发表回答