我将应用程序中的用户位置跟踪到包含所有坐标的数据库。然后我做了一些事情来选择一个时间范围内的坐标范围,但是当我将其保存到服务器时,由于数据量很大,需要很长时间。 (15 分钟相当于 900 个 CLCooperative2D,这相当多了)。
我想要做的是删除与前后坐标相交的坐标。使用过于简单的坐标来进行说明,但想象一下这是在数千个对象的数组中的真实坐标上完成的。
示例:
0,0 //Keep
1,1 //Drop
2,2 //Drop
3,3 //Keep
3,4 //Keep
4,4 //Keep
5,3 //Keep
我知道我可能应该使用一些向量的东西,但我不擅长数学。 如何减少这个数组以删除过时的点?
最佳答案
你可以尝试这样的事情......
var coordTimes:[(coord: CLLocationCoordinate2D, time: Double)] = []
// ...
func appendCoord(newCoord: CLLocationCoordinate2D, newTime: Double) {
guard coordTimes.count > 1 else {
coordTimes.append((newCoord, newTime))
return
}
let n = coordTimes.count
// So there are at least two already in the array
let c0 = coordTimes[n - 2].coord
let t0 = coordTimes[n - 2].time
let c1 = coordTimes[n - 1].coord
let t1 = coordTimes[n - 1].time
let dt = t1 - t0
let dtNew = newTime - t0
guard (dtNew > 0) && (dt > 0) else {
// decide what to do if zero time intervals. Shouldn't happen
return
}
// Scale the deltas by the time interval...
let dLat = (c1.latitude - c0.latitude) / dt
let dLon = (c1.longitude - c0.longitude) / dt
let dLatNew = (newCoord.latitude - c0.latitude) / dtNew
let dLonNew = (newCoord.longitude - c0.longitude) / dtNew
let tolerance = 0.00001 // arbitrary - choose your own
if (abs(dLat - dLatNew) <= tolerance) && (abs(dLon - dLonNew) <= tolerance) {
// Can be interpolated - replace the last one
coordTimes[n - 1] = (newCoord, newTime)
} else {
// Can't be interpolated, append new point
coordTimes.append((newCoord, newTime))
}
}
容差很重要,因为您不太可能获得完全匹配的间隔。另外,对于你们当中的大地测量学家来说,不需要转换成 map 坐标或计算真实距离,因为OP只是想知道坐标是否可以插值。
关于ios - 减少坐标数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385434/