CLLocationManager获取位置很慢,Swift

Nic*_*rbo 7 mapkit cllocationmanager swift location-services

我正在创建这个应用程序,它需要获取用户位置 - 它一切正常,事实上,从接受使用位置服务到获取实际位置需要5秒的时间 - 这是正常的吗?我使用过其他应用程序,速度更快..

这是我的代码的样子:

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")
}
Run Code Online (Sandbox Code Playgroud)

但是从应用程序获取位置放入centerMapOnLocation函数的时间似乎只是很长.获取用户位置时会出现什么情况?我正在测试一个wifi连接,所以我知道它不是因为互联网很慢,或者连接不好......

有人有想法吗?:)

最好的祝福!

Jen*_*ter 8

尝试设置准确性并使用locationManager.startUpdatingLocation().我这样做,并在一秒钟内(在设备上)得到答案.


use*_*248 7

requestLocation()的文档中:

该方法立即返回。调用它会导致位置管理器获取位置修复(可能需要几秒钟)并使用结果调用委托的 locationManager(_:didUpdateLocations:) 方法。

来源

所以基本上,你的代码一切都很好,这就是框架的构建方式。

  • 根据我的经验,与 startUpdatingLocation() 相比,requestLocation() 返回结果的速度要慢得多(2x-3x) (7认同)
  • 是的,根据我的经验,requestLocation() 也比 startUpdatingLocation() 慢得多。我不知道为什么,但我认为这与位置准确性有关。也许 requestLocation 每次都试图超级准确,而 startUpdatingLocation 不太准确?我不知道... (3认同)
  • requestLocation() 很慢,因为它启动位置服务,查找位置,当它找到位置服务时停止。如果您使用 startUpdatingLocation() ,您的位置会经常更新(每 N 秒)并且因此会消耗更多电池。因此,请考虑您如何使用它。如果您使用 startUpdatingLocation() 那么可能会在您收到位置后使用 stopUpdatingLocation() 停止它... (3认同)