首页 文章

按距离排序Google API附近的地点

提问于
浏览
2

我目前仍然坚持这个功能,用户需要附近的位置结果基于距离 .

例如: - 如果我搜索"Kochi"并且我在印度那么kochi在印度和日本那么结果应该像1. Kochi,India 2. Kochi,Japan

这只是一个例子,用户还可以搜索地标,城市,街道等 . 但我希望所有结果按距离排序 . 崩溃结果将首先显示然后显示远处 . 但根据要求无法获得结果 .

类似的功能在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 {
     }
})
  • 使用GooglePlaces.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                         
 })

1 回答

  • 0

    我正在使用相同的GoogleMaps API . 我按照您的要求执行相同的操作,但是通过不同的方式附加代码,我按下“搜索”按钮,您可以将边界设置为坐标或 Map 视图框 . 在下面它当前正在使用用户当前位置的边界,然后它的工作方式从我认为这工作接近完全:

    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)
    

    我遇到了Google方向的问题并获得了计算的距离和里程数,如果您有任何代码可能对我有帮助!

相关问题