0

我已经实现了 google place 自动完成 api,并且我已经应用了界限来将我的自动完成搜索限制在美国佛罗里达州奥兰多市的一个特定城市,但是 Google place api 并没有只过滤这个城市的数据。它正在搜索遍布美国和美国以外的其他城市名称。我希望谷歌自动完成 api 只返回这个城市地址。这是相同的代码:

   let placesClient = GMSPlacesClient()

   let searchBound = getCoordinateBounds(latitude: CLLocationDegrees(28.5383), longitude: CLLocationDegrees(81.3792), distance: 1.45)

    let filter = GMSAutocompleteFilter()
    filter.country = "USA"

    placesClient.autocompleteQuery(text!, bounds: searchBound, filter: filter, callback: { (results, error) in
    })

//我的getCoordinateBounds方法

func getCoordinateBounds(latitude:CLLocationDegrees ,
                         longitude:CLLocationDegrees,
                         distance:Double = 0.001)->GMSCoordinateBounds{
    let center = CLLocationCoordinate2D(latitude: latitude,
                                        longitude: longitude)
    let northEast = CLLocationCoordinate2D(latitude: center.latitude + distance, longitude: center.longitude + distance)
    let southWest = CLLocationCoordinate2D(latitude: center.latitude - distance, longitude: center.longitude - distance)

    return GMSCoordinateBounds(coordinate: northEast,
                               coordinate: southWest)

}

注意:我想过滤这个纬度和经度半径约 100 英里的地址(这就是我将度数设为 1.45 的原因)。在我发现我的边界过滤器无法正常工作后,我添加了国家文件管理器美国的另一件事。到目前为止,我能够将地址搜索限制在美国。

4

1 回答 1

1

您还应该boundsMode在调用中定义参数placesClient.autocompleteQuery()。看看文档:

https://developers.google.com/places/ios-api/reference/interface_g_m_s_places_client

boundsMode必须等于才能GMSAutocompleteBoundsMode.Restrict
边界解释为限制。默认情况下,边界被解释为偏差,这就是您从外部获得结果的原因。

https://developers.google.com/places/ios-api/reference/group___autocomplete_bounds_mode#gafbfae308afa98048c1e97864baa88568

let placesClient = GMSPlacesClient()

let searchBound = getCoordinateBounds(latitude: CLLocationDegrees(28.5383), longitude: CLLocationDegrees(81.3792), distance: 1.45)

let filter = GMSAutocompleteFilter()
filter.country = "USA"

placesClient.autocompleteQuery(text!, bounds: searchBound, boundsMode: GMSAutocompleteBoundsMode.Restrict, filter: filter, callback: { (results, error) in
})

该参数boundsMode在 SDK 2.5 中引入

我希望这有帮助!

于 2018-04-16T09:02:20.803 回答