iOS版。 CLLocationManager只在didUpdateLocations中接收位置更新一次

我有以下代码来获取位置更新(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得到一个更新:调用startTrackingdidUpdateLocations将只被调用一次,并在5秒内GPS指示器消失。

一些细节:

  • 应用程序被授权使用位置服务
  • 应用程序在前台
  • 有趣的是:如果我把断点放在didUpdateLocations它将会被打到didUpdateLocations次。

我已经看到了这里的类似问题的答案(如在Swift中实现CLLocationManagerDelegate方法 ),但它仍然不适用于我。

我究竟做错了什么?

谢谢!

Xcode 6对位置服务做了大量补充。

1)您需要更改您的info.plist文件以包含string“NSLocationWhenInUseUsageDescription”键。 这个价值将成为您开启位置服务的应用推理:“使用位置服务来跟踪您的运行”。

2)使用“[self.locationManager requestWhenInUseAuthorization];” 在你的代码中,使popup窗口出现在你的上面的string中。

更多信息在WWDC 2014video。

这是什么解决了这个问题 – 我已经添加了一个非常短的睡眠didUpdateLocations

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

我同意@Mike,它看起来像一个非常奇怪的修复,但它是一个修复。 如果有人会find更好的解释/答案,请张贴。

尝试实施此方法来查看您的位置pipe理器是否正在暂停:

 func locationManagerDidPauseLocationUpdates(manager: CLLocationManager!) 

如果您正在暂停,请尝试将准确性更改为kCLLocationAccuracyBest