我正在使用Rubymotion开发iOS应用程序。在这个应用程序中,我试图计算行进的距离。我使用下面的代码,在模拟器(高速公路驱动器)中,它返回了很好的数字,但是当我在车里试用它时,它有时会在中途停止计数。
BW::Location.get(distance_filter: 2, desired_accuracy: :best_for_navigation) do |result|
if in_motion(result[:to].speed)
if result[:to].distanceFromLocation(result[:from]) < 40
@distance += result[:to].distanceFromLocation(result[:from])
end
end
end
我也尝试了这个代码,感觉就有更少的东西可以出错? 因为我使用的distance_filter为2,所以我想每次获得新坐标时我只能增加2?这可以吗?
BW::Location.get(distance_filter: 2, desired_accuracy: :best_for_navigation) do |result|
if in_motion(result[:to].speed)
if result[:to].distanceFromLocation(result[:from]) < 40
@distance += 2
end
end
这两种方法输出到不同的数字。
答案 0 :(得分:1)
实际上有一个新的宝石可以让这更容易:
https://github.com/willrax/motion-distance
你这样设置:
@distance = Motion::Distance.new
@distance.activity_type = CLActivityTypeAutomotiveNavigation #for driving
然后你叫'get'来获得行进的距离(以米为单位)
@distance.get do |location|
puts location[:total]
end