i trying calculate distance between 2 points in kilometers giving me wrong.
location.each |loc| distance = haversine.distance(28.6139, 77.209, loc[:lat].to_f, loc[:long].to_f).to_km puts " #{distance}" end
here distance getting , wrong
7385.99072855455 7383.795725224046 7392.13122601482 7391.537885880786
data of location in database
[{"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"}]
you have typo: loc[:long]
instead of loc[:lng]
, line should read:
haversine.distance(28.6139, 77.209, loc[:lat].to_f, loc[:lng].to_f).to_km
you passed nil.to_f
_longitude of second point. equivalent passing 0