Google Places API:Places API无法找到用户的位置

时间:2017-06-11 14:15:57

标签: google-places-api cllocationmanager cllocation

在AppDelegate中,我有以下代码来进行位置跟踪:

    let locationManager = CLLocationManager()
    locationManager.requestWhenInUseAuthorization()
    locationManager.requestAlwaysAuthorization()

在我的ViewController中,我发出了Google商家信息请求:

    placesClient = GMSPlacesClient.shared()
    placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in
        if let error = error {
            print("Pick Place error: \(error.localizedDescription)")
            return
        }

        if let placeLikelihoodList = placeLikelihoodList {
            let place = placeLikelihoodList.likelihoods.first?.place
            if let place = place {
                print("Place:\(place.name)")
            }
        }
    })

错误消息是:

[0](null)“NSLocalizedFailureReason”:“Places API无法找到用户的位置。这可能是因为用户未允许应用程序访问位置信息。”

1 个答案:

答案 0 :(得分:0)

请务必将NSLocationWhenInUseUsageDescription键添加到Info.plist文件中。

<key>NSLocationWhenInUseUsageDescription</key>
<string>Show your location on the map</string>

此外,请为您的模拟器选择一个位置。转到你的模拟器,Debug - &gt;位置 - &gt;自定义位置