【问题标题】:iOS. CLLocationManager receives location update only once in didUpdateLocationsIOS。 CLLocationManager 在 didUpdateLocations 中只接收一次位置更新
【发布时间】:2014-06-23 14:46:26
【问题描述】:

我有以下代码来获取位置更新(iOS 7):

import UIKit
import CoreLocation

class FirstViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!

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

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

    @IBAction func startTracking(sender : AnyObject) {
        NSLog("Start tracking")
        if (locationManager == nil) {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.distanceFilter = kCLDistanceFilterNone
            locationManager.pausesLocationUpdatesAutomatically = false
        }

        locationManager.startUpdatingLocation()
    }

    @IBAction func stopTracking(sender : AnyObject) {
        NSLog("Stop tracking")
        stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        NSLog("Error" + error.description)
    }

    func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
        println("locations = \(locations)")
    }

    func locationManager(manager: CLLocationManager!,
        didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        switch status {
            case CLAuthorizationStatus.Restricted:
                locationStatus = "Access: Restricted"
                break
            case CLAuthorizationStatus.Denied:
                locationStatus = "Access: Denied"
                break
            case CLAuthorizationStatus.NotDetermined:
                locationStatus = "Access: NotDetermined"
                shouldIAllow = true
                break
            default:
                locationStatus = "Access: Allowed"
                shouldIAllow = true
        }
        NSLog(locationStatus)
    }
}

我在didUpdateLocations 中只得到一个更新:在调用startTracking 之后,didUpdateLocations 将只被调用一次,并且在 5 秒内 GPS 指示器消失。

一些细节:

  • 应用被授权使用定位服务
  • 应用程序在前台
  • 有趣的是:如果我在 didUpdateLocations 中设置断点,它将被命中 4 到 5 次。

我在这里看到了类似问题的答案(例如Implement CLLocationManagerDelegate methods in Swift),但它仍然对我不起作用。

我做错了什么?

谢谢!

【问题讨论】:

  • 所以你是说结果只打印了一次,但是断点被打了多次?
  • @Mike 是的,没错。我刚刚将以下行 NSThread.sleepForTimeInterval(1.0) 添加到 didUpdateLocations 处理程序(睡眠线程 1 秒)。现在一切正常。绝对我错过了一些非常愚蠢的东西:(。
  • 这绝对看起来很时髦 - 我不会排除这可能是 Xcode 6 测试版错误的可能性......

标签: ios swift cllocationmanager


【解决方案1】:

Xcode 6 对定位服务做了很多补充。

1) 您需要更改 info.plist 文件以包含字符串“NSLocationWhenInUseUsageDescription”键。这样做的价值将是您的应用程序打开位置服务的推理:“使用位置服务来跟踪您的跑步”。

2) 使用“[self.locationManager requestWhenInUseAuthorization];”在您的代码中以使弹出窗口与您的上述字符串一起出现。

更多信息请参见 WWDC 2014 视频。

【讨论】:

  • 是的,我刚刚对其进行了验证,并且在 Xcode 6 Beta 6 中已修复此问题:以下解决方法:不再需要 NSThread.sleepForTimeInterval(0.001)
【解决方案2】:

这就是解决问题的方法 - 我在 didUpdateLocations 中添加了一个非常短的睡眠:

func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
    println("locations = \(locations)")
    NSThread.sleepForTimeInterval(0.001)
}

我同意@Mike 的观点,它看起来是一个非常奇怪的修复,但它确实是一个修复。如果有人能找到更好的解释/答案,请发布。

【讨论】:

  • 问题已解决,不再需要此解决方法。
【解决方案3】:

尝试实现此方法以查看您的位置管理器是否正在暂停:

func locationManagerDidPauseLocationUpdates(manager: CLLocationManager!)

如果您要暂停,请尝试将准确度更改为 kCLLocationAccuracyBest

【讨论】:

  • 谢谢,@Nate。我没有在此处的代码中提供这些部分,但我确实尝试了locationManagerDidPauseLocationUpdateskCLLocationAccuracyBest。两者都没有解决问题(从未调用过locationManagerDidPauseLocationUpdates)。
【解决方案4】:

Swift 4你可以使用这个,

self.locationManager.requestWhenInUseAuthorization()
self.locationManager.delegate  =  self
self.locationManager.startUpdatingLocation()

【讨论】:

    猜你喜欢
    • 2023-02-16
    • 2017-06-04
    • 2015-05-27
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多