菜鸟教程小白 发表于 2022-12-11 20:16:52

iphone - iOS:locationManager:didUpdateToLocation:fromLocation:不移动时报告速度


                                            <p><p>我正在使用 locationManager:didUpdateToLocation:fromLocation: 来获取位置更新。大多数一切都运行良好,但是当我静止不动时检查 newLocation.speed 属性时,我几乎总是得到大于 0 的速度值。即使我第一次开始获取位置更新并且从未移动过,之后我也会得到正值一些更新。</p>

<p>只需做一个简单的大于 0 的检查,然后设置一个 UILablel(如果是):</p>

<pre><code>if (newLocation.speed &gt; 0) {
   self.speed.text = ];
}
</code></pre>

<p>该方法的更多代码:</p>

<pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    // Check first if we are getting the accuracy we want
    if (newLocation.horizontalAccuracy &lt; 0) return;

    // Make sure the update is new not cached
    NSTimeInterval locationAge = -;
    if (locationAge &gt; 5.0) return;

    // Check to see if old and new are the same
    if ((oldLocation.coordinate.latitude == newLocation.coordinate.latitude) &amp;&amp;
      (oldLocation.coordinate.longitude == newLocation.coordinate.longitude)) return;

    // Make sure our new location is not super far away from old
    CLLocationDistance dist = ;
    NSLog(@&#34;Distance: %f&#34;, dist);
    if (dist &gt; 40) return;

    // Make sure we have a new location
    if (!newLocation) return;

    // Check to see if we are stopped
    if (newLocation.speed == 0) {
      self.inMotion = NO;
    } else if (newLocation.speed &gt; 0) {
      self.inMotion = YES;
    } else {
      // Probably an invalid negative value
      self.inMotion = NO;
    }//end

    // Speed
    // Should always be updated even when not recording!
    if (newLocation.speed &gt; 0 &amp;&amp; inMotion) {
      self.speed.text = ];
      NSLog(@&#34;Speed: %f&#34;, newLocation.speed);
    }

    // Tons more code below that I removed for this post

}//end
</code></pre>

<p>这正常吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>比零大多少?并且持续多久?您正在比较的所有位置是否都具有相同的准确度?</p>

<p>在 GPS 接收器中,速度通常由连续定位之间的位置变化确定(也可以通过测量多普勒频移来计算)。由于测量错误或它们变得更加准确,位置修复会略有不同。位置的变化可以使设备看起来像是移动了​​。</p>

<p>例如,假设您的第一个定位点的水平精度为 1000 米,而您的第二个定位点的水平精度为 300 米,那么第一个位置可能与实际位置相距 1000 米,第二个位置可能与第一个定位点相距 700 米即使设备没有移动。这转化为位置随时间的变化,即“速度”。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - iOS:locationManager:didUpdateToLocation:fromLocation:不移动时报告速度,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7563271/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7563271/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - iOS:locationManager:didUpdateToLocation:fromLocation:不移动时报告速度