【问题标题】:Getting many location updates获取许多位置更新
【发布时间】:2015-02-23 21:15:37
【问题描述】:

我正在使用 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 以使每个请求仅获取一个位置?我如何在动作元素中运行此功能(每个示例的开关或按钮)?最后一个问题,因为也许原因是,在第一个位置请求中,应用程序始终没有将 Accuary 的值作为距离过滤器......谢谢。

【问题讨论】:

    标签: swift cllocationmanager


    【解决方案1】:

    您可以将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) 来检查它返回了多少个位置。如果返回多个位置,通常使用最后一个。

    所以你应该把你的CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in改成locations.last,这样你就可以改用CLGeocoder().reverseGeocodeLocation(locations.last as CLLocation, completionHandler: { (placemarkers, error:NSError!) -> Void in了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多