Haversine没有给我正确的结果

时间:2016-06-07 06:52:39

标签: ruby haversine

我正在尝试计算两公里之间的距离,但是这给了我错误。

location.each do |loc|
  distance =  Haversine.distance(28.6139, 77.209, loc[:lat].to_f, loc[:long].to_f).to_km           
puts "  #{distance}"
end


这是我得到的距离,哪个是错的

 7385.99072855455
 7383.795725224046
7392.13122601482
 7391.537885880786


该位置的数据在我的数据库中

[{"id":1,"lat":28.6139,"lng":77.209,"location":"","object_id":1,"created_at":"2016-06-07T05:46:53.000Z","updated_at":"2016-06-07T05:46:53.000Z","object_type":"abc"},{"id":2,"lat":28.6692,"lng":77.4538,"location":"","object_id":2,"created_at":"2016-06-07T05:49:23.000Z","updated_at":"2016-06-07T05:49:23.000Z","object_type":"cde"},{"id":3,"lat":28.4595,"lng":77.0266,"location":"","object_id":3,"created_at":"2016-06-07T05:50:22.000Z","updated_at":"2016-06-07T05:50:22.000Z","object_type":"ggg"},{"id":4,"lat":28.4744,"lng":77.504,"location":"","object_id":4,"created_at":"2016-06-07T05:50:24.000Z","updated_at":"2016-06-07T05:50:24.000Z","object_type":"eerr"}]

1 个答案:

答案 0 :(得分:2)

你有一个拼写错误:loc[:long]而不是loc[:lng],该行应为:

Haversine.distance(28.6139, 77.209, loc[:lat].to_f, loc[:lng].to_f).to_km

你通过nil.to_f作为第二点的_longitude。这相当于传递0

相关问题