Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the distance between two GPS coordinates without using Google Maps API?

I'm wondering if there's a way to calculate the distance of two GPS coordinates without relying on Google Maps API.

My app may receive the coordinates in float or I would have to do reverse GEO on the addresses.

like image 369
Chim Kan Avatar asked Oct 19 '12 02:10

Chim Kan


People also ask

How do you find the distance between two GPS coordinates?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.


2 Answers

Distance between two coordinates on earth is usually calculated using Haversine formula. This formula takes into consideration earth shape and radius. This is the code I use to calculate distance in meters.

def distance(loc1, loc2)   rad_per_deg = Math::PI/180  # PI / 180   rkm = 6371                  # Earth radius in kilometers   rm = rkm * 1000             # Radius in meters    dlat_rad = (loc2[0]-loc1[0]) * rad_per_deg  # Delta, converted to rad   dlon_rad = (loc2[1]-loc1[1]) * rad_per_deg    lat1_rad, lon1_rad = loc1.map {|i| i * rad_per_deg }   lat2_rad, lon2_rad = loc2.map {|i| i * rad_per_deg }    a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2   c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))    rm * c # Delta in meters end  puts distance([46.3625, 15.114444],[46.055556, 14.508333]) # => 57794.35510874037 
like image 153
Oto Brglez Avatar answered Sep 17 '22 19:09

Oto Brglez


You can use the geokit ruby gem. It does these calculations internally, but also supports resolving addresses via google and other services if you need it to.

require 'geokit'  current_location = Geokit::LatLng.new(37.79363,-122.396116) destination = "37.786217,-122.41619" current_location.distance_to(destination)   # Returns distance in miles: 1.211200074136264 

You can also find out the bearing_to (direction expressed as a float in degrees between 0-360) and midpoint_to (returns an object you can run .latitude and .longitude methods on).

like image 38
Ira Herman Avatar answered Sep 18 '22 19:09

Ira Herman