2016-08-09 29 views
4

你好我在我的IOS應用程序中使用谷歌自動完成的地方。但問題是它沒有工作。我無法獲得用戶的當前位置。獲取用戶的當前位置谷歌API不工作

搜索功能成功地顯示自動完成框的地方,但我遇到的唯一問題是無法得到用戶當前的location.This是獲取用戶當前的位置,我使用

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中添加密鑰以及我正在編譯我的手機上的應用程序。

enter image description here

回答

0

下面的代碼示例檢索的位置列表,其中設備是最容易被定位,並記錄姓名,可能性和其他細節每一個地方。

placesClient.currentPlaceWithCallback({ (placeLikelihoods, error) -> Void in 
    guard error == nil else { 
    print("Current Place error: \(error!.localizedDescription)") 
    return 
    } 

    if let placeLikelihoods = placeLikelihoods { 
    for likelihood in placeLikelihoods.likelihoods { 
     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)") 
    } 
    } 
}) 
+0

我做錯了什麼? – hellosheikh

0

添加以下的viewDidLoad()

placesClient = GMSPlacesClient.shared() 

,你把run()請求授權之後,該API可能無法正常工作的拳頭一次運行該應用程序,因爲它是異步和API將與迴應錯誤,因爲當時還沒有獲得授權。

相關問題