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

时间:2016-01-10 19:19:21

标签: ios swift maps core-location

我有一个应用程序可以获取您的位置并将其打印在标签上。但是我有一个问题,当没有互联网连接时纬度和经度没有显示。这是我的代码。任何解决方案,让经度和经度离线?。为我的语言服务。

@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)
}

1 个答案:

答案 0 :(得分:1)

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

脱离我的头顶(无法访问Xcode):

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