【问题标题】:iOS swift Location Manager not getting updated properlyiOS swift 位置管理器未正确更新
【发布时间】:2016-06-16 10:59:05
【问题描述】:

我正在使用CLLocationManager 获取用户位置。我需要在用户移动时更新用户位置。我正在使用此代码:

import UIKit

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    private var locationManager = CLLocationManager()
    lazy var locations = [CLLocation]()
    var op:String = ""
    @IBOutlet weak var resultTxt: UITextView!

    @IBAction func Start(sender: AnyObject) {
        startLocationManager()
        showResult()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func startLocationManager(){
        locationManager.delegate = self
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    func startLocationTracking() {
        NSLog("startLocationTracking")
        if CLLocationManager.locationServicesEnabled() {
            switch(CLLocationManager.authorizationStatus()) {
            case .NotDetermined, .Restricted, .Denied:
                print("No access")
                self.startLocationManager()
            case .AuthorizedAlways, .AuthorizedWhenInUse:
                NSLog("authorizationStatus authorized")
            }
        } else {
            print("Location services are not enabled")
            self.startLocationManager()
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        for location in locations {
            let howRecent = location.timestamp.timeIntervalSinceNow

            if abs(howRecent) < 10 && location.horizontalAccuracy < 20 {
                //update distance
                self.locations.append(location)
                showResult()
            }
        }
    }

    func showResult(){
        let currentDate = NSDate()
        let res:String = "Result LAT : \(self.locations.last?.coordinate.latitude) AND LNG : \(self.locations.last?.coordinate.longitude)  Time : \(currentDate.toShortTimeString()) \n "
        op += res
        self.resultTxt.text = op
    }

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

}

我已经在.plist 中添加了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription。 这段代码第一次运行良好。但是当用户移动“didUpdateLocations”时不会被解雇。 谁能帮帮我,我做错了什么?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    尝试添加这些行,以便能够在用户移动时监控显着的位置变化:

    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.distanceFilter = 300 //value is up to you
    

    【讨论】:

    • 你试过模拟位置变化吗?可以直接在 iOS 模拟器中完成。
    • 是的...我正在设备上测试它
    • 我明白了,好吧,也许您所做的距离不足以触发位置更新?...
    • 可能是.. 但我使用的是 locationManager.distanceFilter = kCLDistanceFilterNone。我还评论了这一行... if abs(howRecent)
    • 对不起,我找不到 - 你在哪里调用这个函数startLocationTracking()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多