菜鸟教程小白 发表于 2022-12-11 19:47:08

ios - 从 Google API 获取按距离排序的附近地点


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

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

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

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

<p>到目前为止我所尝试的:-</p>

<ul>
<li>使用 GMSPlacesClient.autocompleteQuery 并在其中传递当前位置的边界</li>
</ul>

<pre class="lang-html prettyprint-override"><code>GMSPlacesClient().autocompleteQuery(txtLocation.text!, bounds: bounds, filter: filter, callback: {(results, error) -&gt; Void in
   if let error = error {
         print(&#34;Autocomplete error \(error)&#34;)
         return
   }
   if let results = results {
   }
})
</code></pre>

<ul>
<li>使用 GooglePlaces.placeAutocomplete</li>
</ul>

<pre class="lang-html prettyprint-override"><code>GooglePlaces.placeAutocomplete(forInput: self.txtLocation.text!, offset: 0, locationCoordinate: nil, radius: nil, language: nil, types: , components: nil, completion: { (response, error) in
       // To do                        
})
</code></pre>

<ul>
<li>我也将此网址 (<a href="https://maps.googleapis.com/maps/api/place/autocomplete/json?input=pan&amp;location=30.704316,76.712106&amp;radius=50000&amp;components=country:IN" rel="noreferrer noopener nofollow">https://maps.googleapis.com/maps/api/place/autocomplete/json?input=pan&amp;location=30.704316,76.712106&amp;radius=50000&amp;components=country:IN</a>) 用于 Google API,但对于这些替代方案,我必须进行自定义解析。</li>
</ul></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我正在使用相同的 GoogleMaps API。我按照您的要求做同样的事情,但通过不同的方式附加代码,我在按下“搜索”按钮时执行此操作,您可以将边界设置为坐标或 mapView 框架。在下面,它当前正在使用用户当前位置作为边界,然后它会从我认为这工作得非常完美的地方开始工作:</p>

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>      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)</code></pre>

                                                <p style="font-size: 20px;">关于ios - 从 Google API 获取按距离排序的附近地点,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44283067/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44283067/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从 Google API 获取按距离排序的附近地点