获得许多位置更新

时间:2015-02-23 21:15:38

标签: swift cllocationmanager

我正在使用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或按钮)?最后一个问题,因为可能原因在于,在第一个位置请求中,应用程序并未将Accuary的vaules视为distancefilter ...谢谢。

1 个答案:

答案 0 :(得分:1)

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

distanceFilter用于指定以米为单位的最小更新距离。如果将值设置为500,则每移动500米只会更新一次。默认情况下,使用kCLDistanceFilterNone。通过kCLDistanceFilterNone通知所有动作。

您可以在地理编码器之前使用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)检查它返回的位置数。如果返回的位置不止一个,则通常使用最后一个位置。

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

相关问题