首页 > 解决方案 > 如何使 mostRecentLocation.horizo​​ntalAccuracy 和 hcKalmanFilter?.rValue 可由文本字段编辑?迅速

问题描述

我正在构建一个跟踪应用程序,我发现我应该过滤掉 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

但我仍然收到错误:无法使用类型为“(字符串?)”的参数列表调用类型为“Double”的初始化程序

标签: iosswifttextfieldcllocationmanager

解决方案


因此,在所有最佳实践建议和更正之后,功能是:

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)
            }
        }
    }
}

推荐阅读