如何在Swift中加速从GPS获取坐标位置?

Ale*_*289 6 gps ios swift

我是初学者,我正在制作一个让用户协调的应用程序.我正在制作像下面这样的locationManager类

import UIKit
import CoreLocation


class LocationManager: NSObject {
    let manager = CLLocationManager()
    var didGetLocation: ((Coordinate?) -> Void)?

    override init() {
        super.init()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestLocation()
    }

    func getPermission() {
        // to ask permission to the user by showing an alert (the alert message is available on info.plist)
        if CLLocationManager.authorizationStatus() == .notDetermined {
            manager.requestWhenInUseAuthorization()
        }

    }
}




extension LocationManager : CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            manager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else {
            didGetLocation?(nil)
            return

        }
        let coordinate = Coordinate(location: location)
        if let didGetLocation = didGetLocation {
            didGetLocation(coordinate)

        }
    }
}

private extension Coordinate {
    init(location: CLLocation) {
        latitude = location.coordinate.latitude
        longitude = location.coordinate.longitude
    }
}
Run Code Online (Sandbox Code Playgroud)

我只需要调用didGetLocation属性来获取用户的坐标位置.上面的代码实际上可以得到坐标数据.但我认为这需要太多时间(我在5-7秒后得到了坐标).

说实话我是iOS开发的新手,是否正常获得5-7秒左右的坐标位置?我能改进这个吗?

我怀疑因为我使用的所需精度是kCLLocationAccuracyBest,但如果我改变kCLLocationAccuracyHundredMeters,它似乎是相同的

那么,我可以改进这个吗?因为如果我与android相比,它只是非常快速地获得坐标位置

Rei*_*ian 1

正如我在评论中所说,如果您使用较小的精度值,则可以说kCLLocationAccuracyThreeKilometers您应该更早获得有效位置,但通常需要CLLocationManager一些时间才能获得有效位置,因为大多数位置管理器任务都是异步运行的

Apple 文档对此有何评论

宣言

var desiredAccuracy: CLLocationAccuracy { get set }
Run Code Online (Sandbox Code Playgroud)

讨论

接收器尽最大努力达到所要求的精度;但是,无法保证实际准确性。

您应该为此属性分配一个适合您的使用场景的值。例如,如果您只需要一公里内的当前位置,则应指定 kCLLocationAccuracyKilometerand not kCLLocationAccuracyBestForNavigation更准确地确定位置需要更多的时间和更多的能量

当请求高精度位置数据时,定位服务提供的初始事件可能不具有您所请求的精度。定位服务会尽快传送初始事件。然后,它会继续按照您请求的精度确定位置,并在数据可用时根据需要提供其他事件。

对于 iOS 和 macOS,此属性的默认值为 kCLLocationAccuracyBest。对于 watchOS,默认值为 kCLLocationAccuracyHundredMeters

该属性仅与标准位置服务结合使用,在监视重大位置变化时不使用。