iOS. CLLocationManager receives location update only once in didUpdateLocations

你离开我真会死。 提交于 2019-12-07 13:27:02

问题


I have the following code to get location updates (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.

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.

I have seen answers to the similar questions here (like Implement CLLocationManagerDelegate methods in Swift), but it still doesn't work for me.

What am I doing wrong?

Thanks!


回答1:


Xcode 6 made lots of additions to Location Services.

1) You need to change your info.plist file to include the String "NSLocationWhenInUseUsageDescription" key. 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];" in your code to cause the pop-up to appear with your above string.

More info is on the WWDC 2014 videos.




回答2:


This is what resolved the issue - I've added a very short sleep to the 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. If somebody will find a better explanation/answer, please, post it.




回答3:


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.




回答4:


In Swift 4 you can use this,

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


来源:https://stackoverflow.com/questions/24368866/ios-cllocationmanager-receives-location-update-only-once-in-didupdatelocations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!