将具有确定时间间隔的核心位置GPS数据上传至iOS中的cloud swift
我正在使用core location获取手机的GPS数据,并使用POST请求将其上传到云端 我的代码如下,非常标准:将具有确定时间间隔的核心位置GPS数据上传至iOS中的cloud swift,ios,swift,timer,gps,location,Ios,Swift,Timer,Gps,Location,我正在使用core location获取手机的GPS数据,并使用POST请求将其上传到云端 我的代码如下,非常标准: func getUserLocation() { locationManager = CLLocationManager() locationManager.delegate = self locationManager.distanceFilter = kCLHeadingFilterNone locati
func getUserLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.distanceFilter = kCLHeadingFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.activityType = .automotiveNavigation
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
let lat = String(location.coordinate.latitude)
let lon = String(location.coordinate.longitude)
let iso8601DateFormatter = ISO8601DateFormatter()
iso8601DateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
parameters = [
"timestamp": iso8601DateFormatter.string(from: location.timestamp),
"lat": lat,
"lon": lon
]
print(">>> \(parameters)")
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) { // Change `2.0` to the desired number of seconds.
print(">>> delayed parameters \(self.parameters)")
//gpsManager.uploadGpsData(gpsData: parameters, vehicleId: vehicleId ?? "123123")
}
print(">>>gps data upload successfully")
}
}
我试图使用DispatchQueue.main.asyncAfter
,但它没有达到我的预期
控制台输出:
我不需要所有的GPS坐标(如果是实时的,那真的有很多!我只需要每分钟获取GSP数据