Google Places iOS:如何为API提供上下文以获取相关的本地结果 [英] Google Places iOS: How to provide context to API for relevant local results

查看:52
本文介绍了Google Places iOS:如何为API提供上下文以获取相关的本地结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 iOS版Google Places API ,我正在使用放置自动完成功能.当我开始输入地名时,例如星巴克",它为我提供了星巴克在多个国家/地区的位置,但没有给我本地星巴克的位置.如何将当前位置传递给API,以便搜索结果从最接近我的位置的位置开始?

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?

推荐答案

您可以将 GMSCoordinateBounds 传递到对象,将结果偏向由纬度和经度指定的特定区域边界".

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

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

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

希望这会有所帮助.

这篇关于Google Places iOS:如何为API提供上下文以获取相关的本地结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆