【发布时间】:2019-12-03 12:37:45
【问题描述】:
我正在编写自己的位置过滤器功能,因为我真的发现LocationManager() 选项非常无用.. 非常不精确,并且我在帖子中遵循几十个指南尝试的任何设置总是导致非常糟糕的跟踪......我可怕的意思。。
我实际上是直接在didUpdateLocations 中实现了这个过滤,但是一旦你到达很远,那么后面的任何位置都会被丢弃。除了个人意见,我基本上接受除horizontal accuracy < 0 之外的每个传入位置,然后当我停止获取新位置时,我将数组过滤掉以下参数,如minDistance和maxDistance从以前保存的位置,timestamp,和speed。但是当我在家里稳定地尝试它时,我注意到面团循环良好,它并没有像打印显示的那样过滤掉任何东西,因为我弄错了索引。你能看到我应该如何获得索引吗?一如既往,非常感谢您的帮助。
这是函数:
func filterInvalidLocation(route: [CLLocation]) -> [CLLocation] {
var routeFiltered = route
for location in routeFiltered {
print("processing location is \(location)")
let index = routeFiltered[0].index(ofAccessibilityElement: location)
print("index is : \(index)")
if index > 0 && index <= routeFiltered.capacity{
// invalid
if location.horizontalAccuracy < 0 {
routeFiltered.remove(at: index)
print("location removed : invalid")
}
// too low accuracy
if location.horizontalAccuracy > 80 {
routeFiltered.remove(at: index)
print("location removed : low accuracy")
}
// not sequential
if location.timestamp < routeFiltered[index - 1].timestamp {
routeFiltered.remove(at: index)
print("location removed : not sequential")
}
// too far
if location.distance(from: routeFiltered[index - 1]) > maxDistance {
routeFiltered.remove(at: index)
print("locatione removed : too far")
}
// too close
if location.distance(from: routeFiltered[index - 1]) < minDistance {
routeFiltered.remove(at: index)
print("location removed : too close")
}
// not mooving
if location.speed < 1 {
routeFiltered.remove(at: index)
print("location removed : not mooving")
}
}
}
print("routeFiltered is \(routeFiltered)")
return routeFiltered
}
这是控制台打印:
加工位置 +/- 65.00m(速度 -1.00 mps / course -1.00) @ 03/12/19, 13:16:09 Ora 标准 dell'Europa centrale 指数为:9223372036854775807 处理 位置为 +/- 65.00m(速度 -1.00 mps / course -1.00) @ 03/12/19, 13:16:07 Ora standard dell'Europa centrale 索引是:9223372036854775807 处理位置是 +/- 65.00m(速度 -1.00 mps / 路线 -1.00) @ 03/12/19, 13:16:22 Ora standard dell'Europa centrale index is : 9223372036854775807 处理位置是 +/- 65.00m(速度 -1.00 mps / 路线 -1.00) @ 03/12/19, 13:16:37 Ora 标准 dell'Europa centrale 索引为:9223372036854775807 routeFiltered 为 [ +/- 65.00m(速度 -1.00 mps / 路线 -1.00)@ 03/12/19, 13:16:09 Ora standard dell'Europa centrale, +/- 65.00m (速度 -1.00 mps / 课程 -1.00)@ 03/12/19, 13:16:07 Ora 标准 dell'Europa centrale, +/- 65.00m(速度 -1.00 mps / course -1.00) @ 03/12/19, 13:16:22 Ora standard dell'Europa centrale, +/- 65.00m(速度 -1.00 mps / course -1.00) @ 03/12/19, 13:16:37 Ora standard dell'Europa centrale] MapArray.actualRouteInUseCoordinatesArray.count 是:4
【问题讨论】:
-
index应该包含什么,调用index(ofAccessibilityElement: location)的预期结果是什么?如果它应该是数组中的一个位置,那么index(ofAccessibilityElement: location)与什么有什么关系? -
@JoakimDanielson 是的,你是绝对正确的,这是错误的。我确实需要数组中的位置。
-
index(ofAccessibilityElement:)与firstIndex(where:)? -
@Larme 我试过
if let index = routeFiltered.firstIndex(where: { $0 == location })和if let index = routeFiltered.index(where: { $0 === location })但索引打印function不是Int.. 所以当我尝试读取以前的值时,它会与Thread 1: Fatal error: Index out of range一起崩溃。 -
我不明白你为什么在
routeFiltered[0]上搜索索引,然后直接在routeFiltered上应用/测试。