【问题标题】:Google Place auto complete API bounds not working in swift iOSGoogle Place 自动完成 API 边界在 swift iOS 中不起作用
【发布时间】:2018-09-24 11:28:44
【问题描述】:

我已经实现了 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 的原因)。在我发现我的边界过滤器无法正常工作后,我添加了国家文件管理器 USA 的另一件事。到目前为止,我可以将地址搜索限制在美国。

【问题讨论】:

    标签: ios swift google-places-api google-places googleplacesautocomplete


    【解决方案1】:

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

    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中引入

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多