我正在使用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的價值。