简体   繁体   English

测量两个位置之间的距离不准确

[英]Measure distance between two location inaccurate

am trying to get the distance between two location, but I always get wrong value. 我试图获取两个位置之间的距离,但是我总是得到错误的值。 Here are the codes: 以下是代码:

var startLoction: CLLocation!
var startLoction: CLLocation!

@IBAction func getCurrLocation(){
    startLoction = currLocation

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        currLocation = locations.last!

        if currLocation.horizontalAccuracy > 0{

            var distance = CLLocationDistance(DBL_MAX)
            if let location = self.startLoction {
                distance = currLocation.distance(from: location)
                realTimeDistanceLabel.text = String(format: "%.2f", distance)
            }
        }
}

Problems: 1. When I stay still, the distance sometimes will increase, it may be a big number than 15 meters. 问题:1.当我静止不动时,距离有时会增加,可能比15米大。 It always starts at a big number 2. When I walk out 10 or 20 meters and then walk back straight, the distance sometimes increases but not decreases. 它总是从2开始。当我走出10或20米,然后笔直走回时,距离有时会增加,但不会减少。 3. When I walk around a big circle, the distance goes to a bit more accurate value relatively. 3.当我绕着一个大圆圈走时,距离相对会变得更准确一些。

I also tried to add currLocation.horizontalAccuracy > 0 && currLocation.horizontalAccuracy < 100 , and aslo tried prevLocation >= (currLocation.horizontalAccuracy * 0.5 from stackOverflow answer, still I cannot get a accurate value. 我还尝试添加currLocation.horizontalAccuracy > 0 && currLocation.horizontalAccuracy < 100 ,并且还尝试从stackOverflow答案中获取prevLocation >= (currLocation.horizontalAccuracy * 0.5 ,但仍然无法获得准确的值。

Any other ideas to make it right? 还有其他想法可以解决吗?

Thanks. 谢谢。

Core Location switch on when used, spend a period triangulating the device location such that it takes a while to settle down. 使用“核心位置”时,请打开它,并花一些时间对设备位置进行三角测量,以便花一些时间才能解决。 But also you will find it can lose accuracy in certain locations/conditions and will try always to improve accuracy. 但是您也会发现它在某些位置/条件下可能会失去准确性,并会始终尝试提高准确性。 The device will then be changing it's mind about "where you are located" and if it does this after your method 然后,设备将改变其关于“您所在的位置”的想法,如果它在您的方法之后执行此操作

@IBAction func getCurrLocation() { ... }

is called, naturally it will appear as though the device has moved. 会被调用,自然地,它将看起来好像设备已经移动了。

To reduce the effect of this occurring, you could test not just that currLocation.horizontalAccuracy > 0 (so it is a valid reading) but also that it is less than a given positive value (though this will bring it's own problem - see below). 为了减少这种情况的发生,您不仅可以测试currLocation.horizo​​ntalAccuracy> 0(因此它是一个有效的读数),还可以测试它是否小于给定的正值(尽管这会带来问题,请参阅下文) 。 The current horizontal location of the device may be between plus or minus the radius of uncertainty as reported by the horizontalAccuracy property (the radius of uncertainty is always a positive number. If it is negative, all bets are off, the device is essentially saying "hang on, I can't say where I am with any certainty yet"). 设备当前的水平位置可能介于“ horizo​​ntalAccuracy”属性报告的不确定半径(正负之间)之间(不确定半径始终为正数。如果为负,则所有赌注均无效,该设备实际上是在说“等等,我还不能确定我在哪里”。

Actually though setting an upper bound for currLocation.horizontalAccuracy will demonstrate the problem, it probably won't be what you want because then your UI will only work under the condition Core Location knows accuracy is greater than x. 实际上,尽管为currLocation.horizo​​ntalAccuracy设置上限将演示该问题,但这可能不是您想要的,因为那样,您的UI仅在Core Location知道精度大于x的条件下才能工作。 Generally you wouldn't want to restrict the UI in this way. 通常,您不想以这种方式限制UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM