简体   繁体   English

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

[英]iOS. CLLocationManager receives location update only once in didUpdateLocations

I have the following code to get location updates (iOS 7): 我有以下代码来获取位置更新(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)
    }
}

I get only one update in the didUpdateLocations : after calling startTracking the didUpdateLocations will be called only once and in 5 seconds GPS indicator disappears. 我在didUpdateLocations只获得一次更新:在调用startTrackingdidUpdateLocations将仅被调用一次,并且在5秒内GPS指示符消失。

Some details: 一些细节:

  • application is authorized to use location services 应用程序被授权使用位置服务
  • application is in foreground 应用程序在前台
  • interestingly enough: if I put breakpoint in the didUpdateLocations it will be hit 4 - 5 time. 有趣的是:如果我将断点放在didUpdateLocations它将会被打4到5次。

I have seen answers to the similar questions here (like Implement CLLocationManagerDelegate methods in Swift ), but it still doesn't work for me. 我在这里看到了类似问题的答案(比如在Swift中实现CLLocationManagerDelegate方法 ),但它仍然不适用于我。

What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

Xcode 6 made lots of additions to Location Services. Xcode 6为位置服务做了很多补充。

1) You need to change your info.plist file to include the String "NSLocationWhenInUseUsageDescription" key. 1)您需要更改info.plist文件以包含字符串“NSLocationWhenInUseUsageDescription”键。 The value of this will be your applications reasoning for turning on location services: "Using location services to track your run". 这个值将是您打开位置服务的应用程序的原因:“使用位置服务来跟踪您的运行”。

2) use "[self.locationManager requestWhenInUseAuthorization];" 2)使用“[self.locationManager requestWhenInUseAuthorization];” in your code to cause the pop-up to appear with your above string. 在您的代码中使弹出窗口显示上面的字符串。

More info is on the WWDC 2014 videos. 更多信息在WWDC 2014视频中。

This is what resolved the issue - I've added a very short sleep to the didUpdateLocations : 这就解决了这个问题 - 我为didUpdateLocations添加了一个非常短暂的睡眠:

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

I agree with @Mike that it looks like a very weird fix, but it is a fix. 我同意@Mike认为它看起来像一个非常奇怪的修复,但它是一个修复。 If somebody will find a better explanation/answer, please, post it. 如果有人会找到更好的解释/答案,请发布。

Try implementing this method to see if your location manager is being paused: 尝试实施此方法以查看您的位置管理器是否正在暂停:

func locationManagerDidPauseLocationUpdates(manager: CLLocationManager!)

If you're getting paused, try changing the accuracy to kCLLocationAccuracyBest . 如果您要暂停,请尝试将准确度更改为kCLLocationAccuracyBest

In Swift 4 you can use this, Swift 4你可以使用它,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM