ios - 减少坐标数组

标签 ios swift coordinates

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

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

示例:

0,0 //Keep
1,1 //Drop
2,2 //Drop
3,3 //Keep
3,4 //Keep
4,4 //Keep
5,3 //Keep

或者,糟糕的可视化: enter image description here

我知道我可能应该使用一些向量的东西,但我不擅长数学。 如何减少这个数组以删除过时的点?

最佳答案

你可以尝试这样的事情......

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/

相关文章:

ios - 手动设置日期以使日期选择器始终返回一个日期

iPhone 当前用户位置坐标显示为 (0,0)

iOS:如何显示从 JSON 动态呈现的内容列表?

swift - 仅使用用户的 objectId 检索用户的数据 - Parse & Swift

ios - 在 Map Pin Xcode 中调整 leftCalloutAccessoryView 的 UIImageView 大小

ios - 在单独的文件中设置 UITableView 数据源和委托(delegate) - swift

python - 在 Python 中迭代坐标对

c# - 2 个地理坐标之间的中间点

iphone - 通用应用程序工作流程

objective-c - 将照片上传到 Instagram 后,控件不会返回到应用程序