[英]how to speed up getting coordinate location from GPS in Swift?
I am a beginner, and I am making an app that get user coordinate. 我是初学者,我正在制作一个让用户协调的应用程序。 I am making locationManager class like below
我正在制作像下面这样的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
}
}
I just call didGetLocation
property to get user Coordinate Location. 我只是调用
didGetLocation
属性来获取用户的Coordinate Location。 the code above can get the coordinate data actually. 上面的代码实际上可以得到坐标数据。 but I think it takes too much time (I got the coordinate after 5 - 7 seconds).
但我认为这需要太多时间(我在5-7秒后得到了坐标)。
to be honest I am new in iOS development, is that normal to get coordinate location around 5-7 seconds? 说实话我是iOS开发的新手,是否正常获得5-7秒左右的坐标位置? can I improve this one?
我能改进这个吗?
I suspect because the desired accuracy I used is kCLLocationAccuracyBest
, but if I change to kCLLocationAccuracyHundredMeters
, it seems just the same 我怀疑因为我使用的所需精度是
kCLLocationAccuracyBest
,但如果我改为kCLLocationAccuracyHundredMeters
,它似乎是一样的
so, could I improve this one ? 那么,我可以改进这个吗? Because If I compare to the android, it just very fast to get coordinate location
因为如果我与android相比,它只是非常快速地获得坐标位置
As I said in my comment, If you use minor accuracy value lets say kCLLocationAccuracyThreeKilometers
you should get a valid location earlier but normally the CLLocationManager
take some time to get a valid location because most location manager tasks run asynchronously 正如我在评论中所说,如果您使用较小的准确度值,请说
kCLLocationAccuracyThreeKilometers
您应该提前获得有效位置,但通常CLLocationManager
需要一些时间才能获得有效位置,因为大多数位置管理器任务都是异步运行的
What Apple Docs say about this Apple Docs对此有何评价
Declaration
宣言
var desiredAccuracy: CLLocationAccuracy { get set }
Discussion
讨论
The receiver does its best to achieve the requested accuracy;
接收器尽力达到要求的准确度; however, the actual accuracy is not guaranteed.
但是,实际的准确性并不能保证。
You should assign a value to this property that is appropriate for your usage scenario.
您应该为此属性分配一个适合您的使用方案的值。 For example, if you need the current location only within a kilometer, you should specify
kCLLocationAccuracyKilometer
and notkCLLocationAccuracyBestForNavigation
.例如,如果您只需要一公里内的当前位置,则应指定
kCLLocationAccuracyKilometer
而不是kCLLocationAccuracyBestForNavigation
。 Determining a location with greater accuracy requires more time and more power .确定更精确的位置需要更多时间和更多功率 。
When requesting high-accuracy location data, the initial event delivered by the location service may not have the accuracy you requested.
在请求高精度位置数据时,位置服务提供的初始事件可能没有您请求的准确性。 The location service delivers the initial event as quickly as possible.
定位服务尽快提供初始事件。 It then continues to determine the location with the accuracy you requested and delivers additional events, as necessary, when that data is available.
然后,它会继续以您请求的准确度确定位置,并在数据可用时根据需要提供其他事件。
For iOS and macOS, the default value of this property is
kCLLocationAccuracyBest
.对于iOS和macOS,此属性的默认值为
kCLLocationAccuracyBest
。 For watchOS, the default value iskCLLocationAccuracyHundredMeters
.对于watchOS,默认值为
kCLLocationAccuracyHundredMeters
。This property is used only in conjunction with the standard location services and is not used when monitoring significant location changes.
此属性仅与标准位置服务一起使用,在监视重要位置更改时不使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.