【问题标题】:How to get latitude and longitude while there is no internet connection如何在没有互联网连接的情况下获取纬度和经度
【发布时间】:2016-01-10 19:19:20
【问题描述】:

我有一个应用程序可以获取您的位置并将其打印在标签上。但是当没有互联网连接时,我遇到了一个问题,纬度和经度没有显示。这是我的代码。有什么解决方案可以让纬度和经度脱机吗?。对不起我的语言。

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

【问题讨论】:

    标签: ios swift maps core-location


    【解决方案1】:

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

    在我的脑海中(无法访问 Xcode):

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

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      相关资源
      最近更新 更多