![](/img/trans.png)
[英]Using Swift's Location BLE GPS To Decode location_and_speed
[英]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.