Google 地点 iOS:如何为 API 提供相关本地结果的上下文

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

使用Google Places API for iOS, I am using the Place AutoComplete feature。当我开始输入一个地名时,例如 'Starbucks' 它会给我星巴克在几个国家的位置,但不是我当地的星巴克。如何将当前位置传递给 API,以便搜索结果从离我最近的位置开始?

您可以将 GMSCoordinateBounds 传递给 "object biasing the results to a specific area specified by latitude and longitude bounds"

他们在 site 上有一个示例展示了如何执行此操作:

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)")
    }
})

此示例展示了如何从地图视图中提取此数据,但您也可以手动创建 GMSCoordinateBounds

希望对您有所帮助。