这是我的locationUpdate委托

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


    let lastLocation:CLLocation=locations.last!
    let accuracy:CLLocationAccuracy=lastLocation.horizontalAccuracy// the radius of uncertainity location in meters
    print("Received location \(lastLocation) with acccuracy \(accuracy)")
    if (accuracy < 100.0)
    {
        let span:MKCoordinateSpan=MKCoordinateSpanMake(0.14,0.14)
        let region:MKCoordinateRegion=MKCoordinateRegionMake(lastLocation.coordinate, span)
        self.mapview.setRegion(region, animated: true)
        manager.stopUpdatingLocation()
         print("-------LOCATION UPDATE---------")
        self.getLocationData()

    }

我以这种方式在viewDidLoad中对此进行了调用。
override func viewDidLoad() {
    super.viewDidLoad()
    self.locationManager.delegate=self
    self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.startUpdatingLocation()
}

我的问题是,locationmanager似乎没有停止更新。我的getLocationData()方法起火了2次。

最佳答案

if locations.count > 0 {
    manager.stopUpdatingLocation()
}

if (accuracy < 100.0)
{
    let span:MKCoordinateSpan=MKCoordinateSpanMake(0.14,0.14)
    let region:MKCoordinateRegion=MKCoordinateRegionMake(lastLocation.coordinate, span)
    self.mapview.setRegion(region, animated: true)
    print("-------LOCATION UPDATE---------")
    self.getLocationData()

}
else {
    manager.startUpdatingLocation()
}

这样,您可以确保self.getLocationData()不会被调用,除非精度小于100并且它将被调用一次

关于ios - 位置管理器不会停止更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41779030/

10-12 05:41