2017-05-31 115 views
3

我目前坚持使用此功能,其中用户需要基于距离的附近地点结果。从Google API获取附近的地方按距离排序

对于如: - 如果我搜索“高知”,我在印度 然后科钦是印度以及日本 所以结果应该是像 1.高知,印度 2.高知,日本

这只是一个例子,用户还可以搜索地标,城市,街道等。但我想所有结果由distance.Closer结果进行排序显示,然后再远的地方。但不能按照要求得到结果。

类似的功能在Android上做,他们是通过传递半径(从当前位置像500公里)使用它

是我迄今为止尝试: -

  • 使用GMSPlacesClient.autocompleteQuery和传球在边界它当前的位置
GMSPlacesClient().autocompleteQuery(txtLocation.text!, bounds: bounds, filter: filter, callback: {(results, error) -> Void in 
    if let error = error { 
     print("Autocomplete error \(error)") 
     return 
    } 
    if let results = results { 
    } 
}) 
  • 使用GOOGL ePlaces.placeAutocomplete
GooglePlaces.placeAutocomplete(forInput: self.txtLocation.text!, offset: 0, locationCoordinate: nil, radius: nil, language: nil, types: [GooglePlaces.PlaceType.Regions], components: nil, completion: { (response, error) in 
     // To do       
}) 

回答

0

我在玩同样的GoogleMaps API。我按照你的要求做了同样的事情,但是通过不同的代码方式,我通过按下按钮来搜索'Search',你可以将边界设置为坐标或地图视图框架。在目前它下面是使用用户的当前位置信息的范围,然后出来工作的方式从我认为这完全工作近得:

 let autoCompleteController = GMSAutocompleteViewController() 
 
     autoCompleteController.delegate = self as! GMSAutocompleteViewControllerDelegate 
 
       
 
     // Set bounds to map viewable region 
 
     let visibleRegion = googleMaps.projection.visibleRegion() 
 
     let bounds = GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight) 
 
     
 
     // New Bounds now set to User Location so choose closest destination to user. 
 
     let predictBounds = GMSCoordinateBounds(coordinate: userLocation.coordinate, coordinate: userLocation.coordinate) 
 
     
 
     autoCompleteController.autocompleteBounds = predictBounds 
 
     
 
     // Set autocomplete filter to no filter to include all types of destinations. 
 
     let addressFilter = GMSAutocompleteFilter() 
 
     addressFilter.type = .noFilter 
 
     
 
     autoCompleteController.autocompleteFilter = addressFilter 
 
     
 
     // Change text color 
 
     UISearchBar.appearance().setTextColor(color: UIColor.black) 
 
       
 
     self.present(autoCompleteController, animated: true, completion: nil)

我有一个问题谷歌方向并获得计算的距离和里程,如果你有任何代码可能对我有帮助!