【问题标题】:Location without internet connection没有网络连接的位置
【发布时间】:2016-03-01 19:50:05
【问题描述】:

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

((NSError?) 错误 = 域:“kCLErrorDomain” - 代码:2 { _userInfo = 无})

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

import CoreLocation

class SesionViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager =  CLLocationManager()

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 placemarks!.count > 0 {
                let pm = placemarks![0] as CLPlacemark
                print(pm!.coordinate.latitude)
            }else {
                print("Error with data")
            }

        })
    }
func startButtonPress(sender:UIButton!){
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        self.locationManager.stopUpdatingLocation()
    }
}

编辑

我已经找到了解决方案,我只是不执行 CLGeocoder().reverseGeocodeLocation... 只需像这样使用位置 var 捕获位置

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

【问题讨论】:

  • 您在什么设备上进行测试?并非所有 iOS 设备都有 GPS 硬件,因此有些设备需要互联网连接。
  • 您是否将这两个字符串放入您的 p.list 中,并且您应该使用 status 方法来实际了解您是否被完全授予访问权限
  • 是的,我已经有了这些字符串……问题是我不应该实现与苹果的联系
  • @rmaddy 你能告诉我哪些设备有 GPS 硬件,哪些没有?

标签: ios swift geolocation swift2


【解决方案1】:

来自 Apple 关于 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 单元)

【讨论】:

  • 是的,谢谢...问题是我不需要调用 CLGeocoder().reverseGeocodeLocation,这是我的问题。
猜你喜欢
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 2021-01-01
相关资源
最近更新 更多