OGeek|极客世界-中国程序员成长平台

标题: ios - 从 Google API 获取按距离排序的附近地点 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:47
标题: ios - 从 Google API 获取按距离排序的附近地点

我目前停留在此功能上,用户需要根据距离得出附近地点的结果。

例如:- 如果我搜索“高知”并且我在印度 然后高知在印度和日本 所以结果应该像 1. 印度高知 2. 日本高知

这只是一个例子,用户还可以搜索地标、城市、街道等。但我希望所有结果都按距离排序。更近的结果将首先显示,然后是远的地方。但无法按要求得到结果。

类似的功能是在 android 上完成的,他们通过半径来使用它(比如距离当前位置 500 公里)

到目前为止我所尝试的:-

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(forInput: self.txtLocation.text!, offset: 0, locationCoordinate: nil, radius: nil, language: nil, types: [GooglePlaces.PlaceType.Regions], components: nil, completion: { (response, error) in
       // To do                         
 })



Best Answer-推荐答案


我正在使用相同的 GoogleMaps API。我按照您的要求做同样的事情,但通过不同的方式附加代码,我在按下“搜索”按钮时执行此操作,您可以将边界设置为坐标或 map View 框架。在下面,它当前正在使用用户当前位置作为边界,然后它会从我认为这工作得非常完美的地方开始工作:

        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)

关于ios - 从 Google API 获取按距离排序的附近地点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283067/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4