ios - 如何使 mostRecentLocation.horizontalAccuracy 和 hcKalmanFilter?.rValue 可由文本字段编辑?迅速
问题描述
我正在构建一个跟踪应用程序,我发现我应该过滤掉 GPS 原始数据,所以我使用了卡尔曼滤波器,它确实可以平滑结果跟踪。所以我微调了两个参数,为了能够在 iPhone 上随时更改这些参数,我添加了两个文本字段@IBOutlet weak var filterValueTextField: UITextField!
和@IBOutlet weak var horizontalAccuracyTextField: UITextField
!我想将它们连接到参数hcKalmanFilter?.rValue = 40.0
和guard mostRecentLocation.horizontalAccuracy < 60 else { return }
. 我确实尝试了各种方法,但出现错误:
在此尝试中,无法使用类型为“(String?)”的参数列表调用类型为“Double”的初始化程序:
guard mostRecentLocation.horizontalAccuracy < Double(horizontalAccuracyTextField.text) else { return }
这是功能:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
hcKalmanFilter?.rValue = 40.0
guard let mostRecentLocation = locations.last else { return }
guard mostRecentLocation.horizontalAccuracy > 0 else { return }
guard mostRecentLocation.horizontalAccuracy < 60 else { return }
guard mostRecentLocation.horizontalAccuracy < horizontalAccuracy else { return }
var myLocation: CLLocation = mostRecentLocation
if hcKalmanFilter == nil {
self.hcKalmanFilter = HCKalmanAlgorithm(initialLocation: myLocation)
}
else {
if let hcKalmanFilter = self.hcKalmanFilter {
if resetKalmanFilter == true {
hcKalmanFilter.resetKalman(newStartLocation: myLocation)
resetKalmanFilter = false
}
else {
let kalmanLocation = hcKalmanFilter.processState(currentLocation: myLocation)
self.actualRouteInUseCoordinatesArray.append(kalmanLocation.coordinate)
}
}
}
}
在@maddy 建议之后,我尝试像这样处理它:
var filterValue:Double = 40.0
guard let filterAmount:String? = filterValueTextField.text! else {return}
filterValue = Double(filterAmount)!
hcKalmanFilter?.rValue = filterValue
但我仍然收到错误:无法使用类型为“(字符串?)”的参数列表调用类型为“Double”的初始化程序
解决方案
因此,在所有最佳实践建议和更正之后,功能是:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let filterValue = Double(filterValueTextField.text!)!
hcKalmanFilter?.rValue = filterValue
let horizontalAccuracy = Double(horizontalAccuracyTextField.text!)!
let verticalAccuracy = Double( verticalAccuracyTextField.text!)!
// make sure to get last location
guard let mostRecentLocation = locations.last else { return }
//make sure that it's a valid gps signal ( vertical -1 = no gps triangulation )
guard mostRecentLocation.horizontalAccuracy > 0 else { return }
guard mostRecentLocation.verticalAccuracy > 0 else { return }
// check angainst fine tuning parameters, filtering out locations with errors greater than values
guard mostRecentLocation.horizontalAccuracy < horizontalAccuracy else { return }
guard mostRecentLocation.verticalAccuracy < verticalAccuracy else { return }
// passing the surviving locations to kalman filter for further filtering aiming for precision
var myLocation: CLLocation = mostRecentLocation
if hcKalmanFilter == nil {
self.hcKalmanFilter = HCKalmanAlgorithm(initialLocation: myLocation)
}
else {
if let hcKalmanFilter = self.hcKalmanFilter {
if resetKalmanFilter == true {
hcKalmanFilter.resetKalman(newStartLocation: myLocation)
resetKalmanFilter = false
}
else {
let kalmanLocation = hcKalmanFilter.processState(currentLocation: myLocation)
self.actualRouteInUseCoordinatesArray.append(kalmanLocation.coordinate)
}
}
}
}
推荐阅读
- javascript - 如何在 JavaScript 中从最大值中随机取一个十进制数
- vb.net - 在 .Where(..).Tolist 之后 BindingList 是否仍然是绑定列表
- c - 链表上的指针问题
- android - Gradle 构建错误:协议族不可用
- javafx - 我正在尝试在某种双向链接列表上实现 GUI
- c - 来自用户的字符串输入
- wpf - WPF按钮文本在鼠标悬停时滚动
- javascript - 将 JSON 数据打印到 innerHTML 中变得未定义 - [对象]
- nuxt.js - 使用 ether.js 时减少 webpack 包的大小
- php - How to exclude specific post from wp functions.php code?