如何使 mostRecentLocation.horizontalAccuracy 和 hcKalmanFilter?.rValue 可由文本字段编辑? Swift

How to make mostRecentLocation.horizontalAccuracy, and hcKalmanFilter?.rValue editable by textfields? Swift

我正在构建一个跟踪应用程序,我发现我应该过滤掉 GPS 原始数据,所以我使用了卡尔曼滤波器,它确实平滑了生成的跟踪。所以我微调了两个参数,为了能够从 iPhone 中随时更改这些参数,我添加了两个文本字段 @IBOutlet weak var filterValueTextField: UITextField!@IBOutlet weak var horizontalAccuracyTextField: UITextField!我想将它们连接到参数 hcKalmanFilter?.rValue = 40.0guard 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

但我仍然收到错误消息:无法使用类型为“(String?)”

的参数列表调用类型 '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)
            }
        }
    }
}