如何在没有互联网连接的情况下获取经纬度

How to get latitude and longitude while there is no internet connection

我有一个应用程序可以获取您的位置并将其打印在标签上。但是我有一个问题,当没有互联网连接时,纬度和经度不是 showing.Here 是我的 code.Any 离线获取纬度和经度的解决方案吗?对不起我的语言。

@IBOutlet weak var latitude: UILabel!
@IBOutlet weak var Longitude: UILabel!
let locationManager = CLLocationManager()
override func viewDidLoad() {
    super.viewDidLoad()

}
@IBAction func mapsPressed(sender: UIButton) {

    self.locationsManager()
}

func locationsManager(){
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error) -> Void in
        if (error != nil) {
            print("ERROR:" + error!.localizedDescription)
            return
        }
        if let pm = placemarks?.first {
            self.saveLatitudeLongitude(pm)
            self.locationManager.stopUpdatingLocation()
        }
        else {
            print("Error with data")
        }
    })
}

func saveLatitudeLongitude(placemark: CLPlacemark) {
    //  self.locationManager.stopUpdatingLocation()
    self.latitude.text = String(placemark.location!.coordinate.latitude)
    self.Longitude.text = String(placemark.location!.coordinate.longitude)
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error:" + error.localizedDescription)
}

由于调用了反向地理编码器,您的代码失败了。反向地理编码需要互联网连接。但如果您只需要经纬度,则不需要使用反向地理编码器。坐标应该在 didUpdateLocations 的位置可用。

超出我的想象(无法访问 Xcode):

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print(locations.first?.coordinates)
}