菜鸟教程小白 发表于 2022-12-13 15:51:35

ios - Google Places API 未按预期工作


                                            <p><p>我正在关注 <a href="https://developers.google.com/places/ios-api/start" rel="noreferrer noopener nofollow">Google Places API for IOS</a>查看用户当前位置的教程。</p>

<p>我在教程中使用了相同的代码如下:</p>

<pre><code>var placesClient: GMSPlacesClient!

// Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
@IBOutlet weak var nameLabel: UILabel!

@IBOutlet weak var addressLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    placesClient = GMSPlacesClient.shared()
}

// Add a UIButton in Interface Builder, and connect the action to this function.
@IBAction func getCurrentPlace(_ sender: UIButton) {

placesClient.currentPlace(callback: { (placeLikelihoodList, error) -&gt; Void in
    if let error = error {
      print(&#34;Pick Place error: \(error.localizedDescription)&#34;)
      return
    }

    self.nameLabel.text = &#34;No current place&#34;
    self.addressLabel.text = &#34;&#34;

    if let placeLikelihoodList = placeLikelihoodList {
      let place = placeLikelihoodList.likelihoods.first?.place
      if let place = place {
            self.nameLabel.text = place.name
            self.addressLabel.text = place.formattedAddress?.components(separatedBy: &#34;, &#34;)
                .joined(separator: &#34;\n&#34;)
      }
    }
})
}
</code></pre>

<p>但我在控制台中收到以下错误:</p>

<blockquote>
<p>Pick Place error: The operation couldn’t be completed. The Places API
could not find the user&#39;s location. This may be because the user has
not allowed the application to access location information.</p>
</blockquote>

<p>注意:我在 <code>info.plist</code> 文件中设置了 <code>NSLocationWhenInUseUsageDescription</code> 键(隐私 - 使用时的位置使用说明)。</p>

<p>很迷惑,因为我是按照教程一步一步来的。并且正在使用启用了“位置服务”的物理设备测试应用程序。</p>

<p>知道我可能做错了什么吗?</p>

<p>还是因为文档不是最新的?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><blockquote>
<p>This may be because the user has not allowed the application to access location information.</p>
</blockquote>

<p>这会指向您的答案。要使 Google Places 正常工作,您需要通过调用 <code>requestWhenInUseAuthorization()</code> 请求使用定位服务。这将提示用户授予该应用使用定位服务的权限。</p>

<p>请引用<a href="https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization" rel="noreferrer noopener nofollow" title="Apple docs">Apple Docs</a>了解更多信息。</p>

<p><strong>编辑</strong></p>

<p>您应该保留对您创建的 CLLocationManager 的强引用,这样它就不会在您的函数退出时被释放。</p>

<blockquote>
<p>&#34;Create an instance of the CLLocationManager class and store a strong reference to it somewhere in your app.
Keeping a strong reference to the location manager object is required until all tasks involving that object are complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient.&#34;</p>
</blockquote>

<p>取自<a href="https://developer.apple.com/documentation/corelocation/cllocationmanager" rel="noreferrer noopener nofollow">CLLocationManager Docs</a> </p>

<p><strong>示例</strong></p>

<pre><code>class LocationViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()

override func viewDidLoad()
{
    super.viewDidLoad()

    locationManager.delegate = self
    if CLLocationManager.authorizationStatus() == .notDetermined
    {
       locationManager.requestWhenInUseAuthorization()
    }
}
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Google Places API 未按预期工作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47032927/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47032927/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Google Places API 未按预期工作