2015-02-23 73 views
1

我正在使用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的距離過濾器...... Thankyou的價值。

回答

1

您可以將self.locationManager.startUpdatingLocation()放在您的actionLocation()函數中,以便在您按下按鈕後它將開始獲取新位置。

distanceFilter用於指定以米爲單位的最小更新距離。如果將該值設置爲500,則只會更新每移動500米。默認情況下,使用kCLDistanceFilterNone。通過kCLDistanceFilterNone通知所有動作。

您可以在地理編碼之前使用布爾值,之後或您的displayLocationInfo方法內改變布爾值

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