没有互联网连接的位置

时间:2015-11-27 15:12:56

标签: ios swift geolocation swift2

我想在按下按钮时获取用户的位置,我找到了一种方法来使用CLLocationManager,该代码完美地工作,直到设备没有网络连接,函数func locationManager( manager:CLLocationManager,didUpdateLocations位置:[CLLocation])捕获错误

  

((NSError?)error = domain:" kCLErrorDomain" - code:2 {_userInfo =   nil})

如何在没有互联网连接的情况下获取设备位置?这是我使用的代码

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print(manager.location!.coordinate.latitude)
}

修改

我已经找到了解决方案,我只是不执行CLGeocoder()。reverseGeocodeLocation ...只是抓住位置var这样的位置

div

1 个答案:

答案 0 :(得分:0)

来自Apple doc的关于ClGeocoder的

  

计算机或设备必须能够访问网络才能进入   地理编码器对象返回详细的地标信息

CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
            if (error != nil) {
                print("Error:" + error!.localizedDescription)
                return
            }
            if placemarks!.count > 0 {
                let pm = placemarks![0] as CLPlacemark
                print(pm!.coordinate.latitude)
            }else {
                print("Error with data")
            }

        })
如果没有互联网访问,

将始终报告错误。您的位置数据仍然可用(如果您的设备具有GPS设备)

相关问题