【问题标题】:How to implement CLLocationManager.requestLocation() on iOS 8 or older?如何在 iOS 8 或更早版本上实现 CLLocationManager.requestLocation()?
【发布时间】:2015-09-04 10:13:22
【问题描述】:

如果您已阅读新的 iOS 9 文档,您可能已经注意到一种新方法,它可以让位置管理器在短时间内启动无线电,修复您的位置并在所需的准确度(或超时)到达时,将其全部关闭,将精确定位的位置交给代表。

该方法名为CLLocationManager.requestLocation(),可在 iOS 9 SDK 中使用。唉,我目前正在开发一个针对 iOS 8 的应用程序,并且仍然非常想使用这种方法。我也不想自己重新实现它。

所以这是我的问题:是否有任何适用于 iOS 的开源库实现这种一次性位置检索?

【问题讨论】:

  • 您可能喜欢使用“INTULocationManager”
  • 正是我想要的,谢谢 :)

标签: ios objective-c swift cllocationmanager


【解决方案1】:

你需要先执行两个步骤

1) NSLocationWhenInUseUsageDescription

2) NSLocationAlwaysUsageDescription

然后

var locationManager = CLLocationManager()
    locationManager.delegate = self
    // locationManager.locationServicesEnabled
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    let Device = UIDevice.currentDevice()

    let iosVersion = NSString(string: Device.systemVersion).doubleValue

    let iOS8 = iosVersion >= 8

    if iOS8{

        locationManager.requestWhenInUseAuthorization()
    }
else{
locationManager.requestAlwaysAuthorization()
}

locationManager.startUpdatingLocation()

这会有帮助。谢谢你

【讨论】:

  • 不幸的是,这不是我想要的。开始位置更新后,我需要等待一段时间才能使准确度达到可接受的水平,或者在超时触发时取消。
【解决方案2】:

正如 Arslan Asim 指出的那样,一个好的解决方案是使用INTULocationManager

来自文档:

manager.requestLocationWithDesiredAccuracy(.Block, timeout: 10.0) {
    (currentLocation, achievedAccuracy, status) in
    // Examine status to see if the request timed out.
    // If not, currentLocation stores the found location.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2013-07-12
    • 2012-11-26
    • 2014-12-03
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多