[英]Get user's current location google api not working
你好我在我的IOS應用程序中使用谷歌自動完成位置。 但問題是它不起作用。 而且我無法獲得用戶的當前位置。
搜索功能成功顯示自動填充框中的位置,但我遇到的唯一問題是無法獲取用戶的當前位置。這是獲取用戶當前位置的代碼我正在使用
class ViewController: UIViewController,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var placesClient : GMSPlacesClient?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
run()
}
func run(){
self.placesClient?.currentPlaceWithCallback({ (list: GMSPlaceLikelihoodList?, error: NSError?) -> Void in
if let error = error {
print("error: \(error.description)")
return
}
for likelihood in list!.likelihoods {
if let likelihood = likelihood as? GMSPlaceLikelihood {
let place = likelihood.place
print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)")
print("Current Place address \(place.formattedAddress)")
print("Current Place attributions \(place.attributions)")
print("Current PlaceID \(place.placeID)")
}
}
})
}
我也在info.plist中添加了密鑰,我正在手機上編譯應用程序。
在viewDidLoad()
添加以下內容
placesClient = GMSPlacesClient.shared()
並且您在請求授權后立即放入run()
,因為它是異步的,API可能無法在您運行應用程序時第一次運行,並且API會因為當時尚未獲得授權而響應錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.