Ios 减少坐标数组

Ios 减少坐标数组,ios,swift,coordinates,Ios,Swift,Coordinates,我在我的应用程序中将用户的位置跟踪到一个包含所有坐标的数据库中。然后,我做了一些事情来选择一个时间范围内的坐标,但当我将其保存到服务器时,由于数据量大,需要很长时间。(15分钟是900CLCoordinate2D,这相当多) 我想做的是删除前面和后面的坐标相交的坐标。使用过于简单的坐标进行说明,但想象一下这是在一个包含数千个对象的数组中的真实坐标上进行的 例如: 0,0 //Keep 1,1 //Drop 2,2 //Drop 3,3 //Keep 3,4 //Keep 4,4 //Keep 5

我在我的应用程序中将用户的位置跟踪到一个包含所有坐标的数据库中。然后,我做了一些事情来选择一个时间范围内的坐标,但当我将其保存到服务器时,由于数据量大,需要很长时间。(15分钟是900
CLCoordinate2D
,这相当多)

我想做的是删除前面和后面的坐标相交的坐标。使用过于简单的坐标进行说明,但想象一下这是在一个包含数千个对象的数组中的真实坐标上进行的

例如:

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))
    }
}
var坐标时间:[(坐标:CLLocationCoordinate2D,时间:Double)]=[]
// ...
func AppendCoords(newCoords:CLLocationCoordinate2D,newTime:Double){
guard coordTimes.count>1其他{
追加((newCoord,newTime))
返回
}
设n=coordTimes.count
//因此,阵列中至少已经有两个
设c0=coordTimes[n-2]。coord
设t0=coordTimes[n-2]。时间
设c1=coordTimes[n-1]。coord
设t1=coordTimes[n-1]。时间
设dt=t1-t0
设dtNew=newTime-t0
保护(dtNew>0)和(dt>0)其他{
//如果时间间隔为零,决定该怎么做。不应该发生
返回
}
//按时间间隔缩放三角洲。。。
设dLat=(c1.纬度-c0.纬度)/dt
设dLon=(c1.经度-c0.经度)/dt
设dLatNew=(newCoord.latitude-c0.latitude)/dtNew
设dLonNew=(newCoord.longitude-c0.longitude)/dtNew
让公差=0.00001//任意-选择自己的公差

if(防抱死制动系统(dLat-dLatNew)它们的时间间隔是否均匀?带BestForNavigation的位置管理器大约每1秒更新一次。这看起来很有希望,会立即进行测试。但我看不出时间的相关性。也许我应该更具描述性,但位置已经保存并从最早的位置到最新的位置进行排序,而不是动态追加。如果是这样的话在这种情况下,只需将时间替换为原始索引号(而不是压缩索引号)