【问题标题】:iOS - The best way to measure distanceiOS - 测量距离的最佳方式
【发布时间】:2017-11-19 05:51:24
【问题描述】:

我目前正在做一个项目,我需要用户告诉我在哪里(在现实世界的地图上)建造一堵墙。

问题:(您认为)用户显示(/告诉)墙的位置最准确的方式是什么?

想法 1 我曾想过在地图上画图,但这不会那么准确。

想法 2 我想到的另一件事是,用户应该将他们的手机放在墙壁的开头和结尾。通过这种方式,应用程序可以使用CLLocationManager 打印地图上的位置,并测量两端之间的距离。

这是我尝试过的代码,但它根本不是那么准确。

let locationManager = CLLocationManager()

var location1: CLLocation = CLLocation()
var location2: CLLocation = CLLocation()

override func viewDidLoad() {
    super.viewDidLoad()
    setup()
}

func setup() {
        resett.isHidden = true

        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.activityType = .fitness
            locationManager.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        currentCord.text = "\(locValue.latitude) \(locValue.longitude)"
    }

    func measure(cord1: CLLocation, cord2: CLLocation) -> Float {
        let distanceInMeters = cord1.distance(from: cord2) //result is in meters
        return Float(distanceInMeters)
    }

    func calculateCoordinates(status: Int) {
        let coordinate = calculate()

        if status == 1 {
            location2 = coordinate

            let measured = measure(cord1: location1, cord2: location2)
            print("\(measured) m")
            displayLabel.text = "\(measured)m"
            resett.isHidden = false
        } else {
            location1 = coordinate
        }
    }

    func calculate() -> CLLocation {
        let locValue:CLLocationCoordinate2D = locationManager.location!.coordinate
        let coordinate = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)

        return coordinate
    }

    @IBAction func FirstAction(_ sender: Any) {
        button1.setTitle("Klar", for: .normal)
        calculateCoordinates(status: 0)
    }

    @IBAction func SecondAction(_ sender: Any) {
        button2.setTitle("Klar", for: .normal)
        calculateCoordinates(status: 1)
    }

提前致谢!

【问题讨论】:

  • 您需要提供更多信息并更清楚,您面临的具体问题是什么? CLLocationManager、“测量距离”和“在地图上绘图”并没有真正的关联
  • 抱歉,已经更新问题了。

标签: ios swift mapkit cllocationmanager concept


【解决方案1】:

不幸的是,iPhone 上的 GPS 不是很准确。如果您可以在 32 米半径范围内获得准确的位置,那么您就做得很好,而且那是在一个开阔的区域,可以清晰地看到天空,并且没有无线电干扰。如果情况不太理想,那么结果可能会不太准确。

(Apple 报告其 GPS 定位结果中的“水平精度”读数实际上是误差范围的半径。)

为了建造一堵墙,32 米根本不够好,iPhone 中的 GPS 无法可靠地做得更好。你真的需要测量设备。

【讨论】:

    【解决方案2】:

    假设您指的是现实世界的“墙”,尺寸为厘米到几米,iPhone GPS 的位置确实不适合此类测量,这是良好覆盖区域的典型最小误差5 米

    如果明确询问地理坐标(精度非常高)是不可行的: 我宁愿明确询问用户尺寸,然后可能在地图上拖动对象(墙),在接近可能的最终位置时尽可能地缩放。

    PS:无论如何,可以进行许多优化以提高CLLocationManager 的准确性,首先过滤掉accuracy 较低的结果,并在收到后手动清除horizzontal accuracy 问题较大的结果它。

    【讨论】:

    • 您认为可以先询问用户尺寸,然后使用 ARKit 将墙放置在地图上吗?
    • 是的,我很确定这不应该是一个问题,因为你有一些坚实的 AR 基础:) 用户应该能够拖动/移动/命令墙壁,它会相应地呈现观点。您最终可以使用该位置来近似墙的初始位置,然后让用户根据您的需要对其进行优化。
    • 我会尝试做一个演示。完成后我会看看是否可以在这里发布github链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多