2017-05-31 114 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)

我有一個問題谷歌方向並獲得計算的距離和里程,如果你有任何代碼可能對我有幫助!