在創建這個應用程序時,它需要獲取用戶的位置 - 它的所有工作都正常,事情是,從接受位置服務的使用到獲取實際位置所需的時間像5秒鐘 - 這是正常的嗎? 我使用其他應用程序,哪裏就有奇蹟快得多..CLLocationManager獲取位置的速度很慢,Swift
這裏是我的代碼如下所示:
override func viewDidLoad() {
super.viewDidLoad()
// Ask for Location-Authorisation from the User.
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.requestLocation()
}
mapView.delegate = self
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue: CLLocationCoordinate2D = manager.location!.coordinate
let initialLocation = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
self.centerMapOnLocation(initialLocation)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("could not get location")
}
但是從應用程序的時間得到的位置裝進centerMapOnLocation功能,似乎只是很長。在獲得用戶位置時會發生什麼?我正在測試一個wifi連接,所以我知道它不是因爲互聯網很慢或者它的連接不好...
任何人都有想法? :)
最好的問候!
嘗試設置準確性並使用locationManager.startUpdatingLocation()。我這樣做,並在一秒鐘之內得到答案(在設備上)。 –
太棒了! locationManager.startUpdatingLocation()做到了!我用.requestLocation()替換它,現在它的工作就像一個魅力!謝謝@JensPeter - 想給你正確的答案,但我不能作爲它的評論:/寫一個答案,我會接受它:) –