我正在做iOS应用程序关联用户的位置 .

我设置CLLocationManager并且它运行良好,但是在CLLocationManager上调用stopUpdatingLocation()之后,委托方法“didUpdateLocations”仍然被调用了几次 . 而且这并不总是发生 .

这是因为stopUpdatingLocation()调用和locationManager之间的延迟真的停止了吗?

这就是我的CLLocationManager设置的方式:

final class LocationController: NSObject {

    typealias Action = (CLLocation) -> ()

    static let shareInstance = LocationController()

    private let locationManager = CLLocationManager()

    private var location: CLLocation?

    private var action: Action?

    private var timer: NSTimer?

    private override init () {

        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
        locationManager.activityType = .Fitness
    }

}

这是我的代表团方法:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let newLocation = locations.last!
    let accuracy = newLocation.horizontalAccuracy

    // horizontalAccuracy less than 0 is invalid result, ignore it
    guard accuracy >= 0 else { return }

    // ignore the result that accuracy less than desiredAccuracy
    guard accuracy <= locationManager.desiredAccuracy else { return }

    // accept the result
    self.location = newLocation

    // stop locating
    self.stopLocationManager()
}

这就是我停止locationManager的方法:

func stopLocationManager() {

    self.locationManager.stopUpdatingLocation()
    print("the location manager should be stopped!!!!!!!!!!!")

    if let timer = self.timer {
        timer.invalidate()
    }

    guard let action = self.action else { return }

    if let location = self.location {
        action(location)
    } else {
        action(TomoConst.Geo.CLLocationTokyo)
    }

    self.action = nil
    self.location = nil
}

这就是我使用LocationManager的方式:

func doActionWithLocation(action: Action) {

    self.action = action

    guard self.determineStatus() else { return }

    self.timer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: Selector("stopLocationManager"), userInfo: nil, repeats: false)

    self.locationManager.startUpdatingLocation()
    print("the location manager started!!!!!!!!!!!")
}

结果,我看到“位置经理开始了!”一次,然后看到“应该停止位置管理员!”多次 . 我认为停止消息应该只打印一次 . 我调试了很长时间没有运气 .

请有人告诉我为什么......

提前致谢 .

顺便说一句,如果我以极其简单的方式设置所有内容,则LocationManager正确停止:

let locman = CLLocationManager()
var count = 1

override func viewDidLoad() {
    super.viewDidLoad()
    locman.delegate = self
    locman.desiredAccuracy = kCLLocationAccuracyHundredMeters
    locman.activityType = .Fitness
    locman.requestWhenInUseAuthorization()
    locman.startUpdatingLocation()

    print("start")
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("\(count) \(locations.last)")
    if self.count == 5 {
        locman.stopUpdatingLocation()
    }
    count++
}

上面的代码打印消息5次,似乎没有进一步调用didUpdateLocations .