获取许多位置更新

Getting many location updates

我正在使用 swift 实现 IOS 的用户位置。这是我的代码。

import UIKit
import CoreLocation

class localizationViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.distanceFilter = 500
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
    }

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

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in

            if (error != nil) {
                println("Error: " + error.localizedDescription)
                return
            }

            if placemarks.count > 0 {

                let pm = placemarks[0] as CLPlacemark
                self.displayLocationInfo(pm)
            } else {
                println("Error with data")
            }
        })
    }

    func displayLocationInfo(placemark: CLPlacemark) {
        self.locationManager.stopUpdatingLocation()

        var city = placemark.locality
        var department = placemark.administrativeArea
        var country = placemark.country
        var latitude = placemark.location.coordinate.latitude
        var longitude = placemark.location.coordinate.longitude
        var date = placemark.location.timestamp

        println(city)
        println(department)
        println(country)
        println(latitude)
        println(longitude)
        println(date)
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("Error: " + error.localizedDescription)
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using    segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */


    @IBAction func actionLocation(sender: UISwitch) {
        println("get location request")
    }
}

我正在正确获取位置信息,但总是更新 3 或 4 次。我在读到如果你设置 distanceFilter 这会停止但是,它对我不起作用。我如何配置我的 didUpdateLocations 以便每个请求只获取一个位置?我如何 运行 在 action 元素(每个示例中的 switct 或 button)中使用此功能?最后一个问题,因为也许原因是,在第一次位置请求时,应用程序不会将 Accuary 的值作为距离过滤器……谢谢。

您可以将 self.locationManager.startUpdatingLocation() 放入 actionLocation() 函数中,以便在您按下按钮后它会开始获取新位置。

distanceFilter是指定以米为单位的最小更新距离。如果您将该值设置为 500,它只会在您移动 500 米时更新一次。默认情况下,使用 kCLDistanceFilterNone。传入 kCLDistanceFilterNone 以通知所有移动。

您可以在 Geocoder 之前使用 Bool 值,并在 displayLocationInfo 方法之后或内部更改 Bool 值

if updating == false {
  updating = true

CLGeocoder().reverseGeocodeLocation(locations.last as CLLocation, completionHandler: { (placemarkers, error:NSError!) -> Void in 
  .....
   self.displayLocationInfo(pm)
   self.updating = false
})
}

在您的 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) 中,您应该 println(locations.count) 检查它有多少位置 returns。如果返回多个位置,通常使用最后一个。

因此您应该将 CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in 更改为使用 locations.last,这样您就可以使用 CLGeocoder().reverseGeocodeLocation(locations.last as CLLocation, completionHandler: { (placemarkers, error:NSError!) -> Void in