我是初学者,我正在制作一个获取用户坐标的应用程序。我正在制作如下所示的 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
属性来获取用户坐标位置。上面的代码实际上可以得到坐标数据。但我认为这需要太多时间(我在 5 - 7 秒后得到了坐标)。
老实说,我是iOS开发的新手,5-7秒左右获取坐标位置正常吗?我可以改进这个吗?
我怀疑是因为我使用的所需精度是kCLLocationAccuracyBest
,但如果我更改为kCLLocationAccuracyHundredMeters
,它似乎是一样的
那么,我可以改进这个吗?因为如果我与android相比,它获取坐标位置的速度非常快
最佳答案
正如我在评论中所说,如果您使用较小的精度值,假设 kCLLocationAccuracyThreeKilometers
您应该更早获得有效位置,但通常 CLLocationManager
需要一些时间才能获得有效位置,因为大多数位置管理器任务异步运行
Apple 文档对此有何评论
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
. 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
. For watchOS, the default value iskCLLocationAccuracyHundredMeters
.This property is used only in conjunction with the standard location services and is not used when monitoring significant location changes.
关于ios - 如何加快在 Swift 中从 GPS 获取坐标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49127564/