簡體   English   中英

如何在Swift中加速從GPS獲取坐標位置?

[英]how to speed up getting coordinate location from GPS in 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
    }
}

我只是調用didGetLocation屬性來獲取用戶的Coordinate Location。 上面的代碼實際上可以得到坐標數據。 但我認為這需要太多時間(我在5-7秒后得到了坐標)。

說實話我是iOS開發的新手,是否正常獲得5-7秒左右的坐標位置? 我能改進這個嗎?

我懷疑因為我使用的所需精度是kCLLocationAccuracyBest ,但如果我改為kCLLocationAccuracyHundredMeters ,它似乎是一樣的

那么,我可以改進這個嗎? 因為如果我與android相比,它只是非常快速地獲得坐標位置

正如我在評論中所說,如果您使用較小的准確度值,請說kCLLocationAccuracyThreeKilometers您應該提前獲得有效位置,但通常CLLocationManager需要一些時間才能獲得有效位置,因為大多數位置管理器任務都是異步運行的

Apple Docs對此有何評價

宣言

var desiredAccuracy: CLLocationAccuracy { get set }

討論

接收器盡力達到要求的准確度; 但是,實際的准確性並不能保證。

您應該為此屬性分配一個適合您的使用方案的值。 例如,如果您只需要一公里內的當前位置,則應指定kCLLocationAccuracyKilometer而不是kCLLocationAccuracyBestForNavigation 確定更精確的位置需要更多時間和更多功率

在請求高精度位置數據時,位置服務提供的初始事件可能沒有您請求的准確性。 定位服務盡快提供初始事件。 然后,它會繼續以您請求的准確度確定位置,並在數據可用時根據需要提供其他事件。

對於iOS和macOS,此屬性的默認值為kCLLocationAccuracyBest 對於watchOS,默認值為kCLLocationAccuracyHundredMeters

此屬性僅與標准位置服務一起使用,在監視重要位置更改時不使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM