我正在跟随 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")
}
}
})
}
但是我在控制台中收到以下错误:
选择位置错误:无法完成操作。地点 API 无法找到用户的位置。这可能是因为用户未允许应用程序访问位置信息。
注意:我已经在 info.plist
文件中设置了 NSLocationWhenInUseUsageDescription
键(隐私 - 使用时位置描述)。
这很令人困惑,因为我按照教程逐步进行操作,并使用启用了“位置服务”的物理设备测试应用程序。
有任何想法我可能做错了什么吗?
还是因为文档不是最新的?