【问题标题】:Get nearby places from Google API sorted by distance从 Google API 获取按距离排序的附近地点
【发布时间】:2017-05-31 11:04:36
【问题描述】:

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

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

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

类似的功能是在 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                         
 })

【问题讨论】:

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


    【解决方案1】:

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

            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 路线和获取计算出的距离和里程时遇到问题!

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 2012-05-30
      相关资源
      最近更新 更多