如何在Swift离线时从GPS获取位置?

时间:2016-01-13 10:21:20

标签: ios gps location

我想让应用程序可以在没有互联网的情况下获得位置(纬度和经度),有人告诉我使用CCLocationManager,here

我用我的代码进行测试:

var locationManager:CLLocationManager!

@IBOutlet weak var locationTextField: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
    let authstate = CLLocationManager.authorizationStatus()
    if(authstate == CLAuthorizationStatus.NotDetermined){
        print("Not Authorised")
        locationManager.requestWhenInUseAuthorization()
    }
    print("first")
    locationTextField.text = "11"

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("success \(locations.count)")
    let location:CLLocation = locations[locations.count - 1] as CLLocation
    print("aa:\(location.coordinate.latitude) \(location.coordinate.longitude)aa")
    locationTextField.text = location.description

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Couldn't get your location with \(error.description)")
}

但它只适用于互联网,当我关闭wifi时,我的应用程序日志:Couldn't get your location with Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"

也许我错了,有任何帮助,谢谢。

0 个答案:

没有答案