Created
September 30, 2020 08:13
-
-
Save dereknguyen269/9f52a7d1d5944943060dc847455a136d to your computer and use it in GitHub Desktop.
Distance between two points on earth with Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.geeksforgeeks.org/program-distance-two-points-earth/ | |
earthRadius = 6371 # km | |
d_lat = (origin[:lat] - destination[:lat]) / 180.0 * Math::PI | |
d_lng = (origin[:lng] - destination[:lng]) / 180.0 * Math::PI | |
sin_lat = Math.sin(d_lat / 2) | |
sin_lng = Math.sin(d_lng / 2) | |
rad_lat1 = origin[:lat] / 180.0 * Math::PI | |
rad_lat2 = destination[:lat] / 180.0 * Math::PI | |
a = (sin_lat * sin_lat) + (sin_lng * sin_lng) * Math.cos(rad_lat1) * Math.cos(rad_lat2) | |
c = 2 * Math.asin(Math.sqrt(a)) | |
distance = earthRadius * c # Distance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment