Google 个地方 API 没有按预期工作

Google places API doesn't work as expected

我正在按照Google Places API for IOS教程查看用户当前位置。

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

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) -> Void in
    if let error = error {
        print("Pick Place error: \(error.localizedDescription)")
        return
    }

    self.nameLabel.text = "No current place"
    self.addressLabel.text = ""

    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: ", ")
                .joined(separator: "\n")
        }
    }
})
}

但我在控制台中收到以下错误:

Pick Place error: The operation couldn’t be completed. The Places API could not find the user's location. This may be because the user has not allowed the application to access location information.

注意:我在 info.plist 文件中设置了 NSLocationWhenInUseUsageDescription 密钥(隐私 - 使用时的位置使用说明)。

很迷惑,因为我是按照教程一步一步来的。并且正在使用启用了 "Locations Services" 的物理设备测试应用程序。

知道我做错了什么吗?

还是因为文档不是最新的?

This may be because the user has not allowed the application to access location information.

这会指向您的答案。对于 Google 个工作地点,您需要致电 requestWhenInUseAuthorization() 请求使用定位服务。这将提示用户授予该应用程序使用位置服务的权限。

更多信息请参考Apple Docs

编辑

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

"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."

取自CLLocationManager Docs

示例

class LocationViewController: UIViewController, CLLocationManagerDelegate { 
  let locationManager = CLLocationManager()

  override func viewDidLoad()
  {
    super.viewDidLoad()

    locationManager.delegate = self
    if CLLocationManager.authorizationStatus() == .notDetermined
    {
       locationManager.requestWhenInUseAuthorization()
    }
  }
}

1.请求用户位置使用授权

requestWhenInUseAuthorization()requestAlwaysAuthorization() 根据您的要求。

2.Info.plist中添加如下键值:

一个。 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

b。 NSLocationAlwaysAndWhenInUseUsageDescription

示例:

class ViewController: UIViewController, CLLocationManagerDelegate
{
    let locationManager = CLLocationManager()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        locationManager.requestWhenInUseAuthorization()
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
    {

    }
}