user1051849
user1051849

Reputation: 2337

Rails/ruby Calculate new Coordinates using Given Distance and Bearing from a Start Point

Is there a simple, non-math-class and definitive way to do this in ruby/rails where the inputs are:

1) An Initial Geo-Coordinate 
2) A Bearing
3) A Distance in the Bearing's Direction

And the Output is a new Geo-Coordinate

I've had a look at the Ruby Geocoder Gem, but it doesn't do this.

Thanks!

Upvotes: 4

Views: 2039

Answers (1)

Stefan
Stefan

Reputation: 114178

First hit for "distance bearing" in Google: Calculate distance, bearing and more between Latitude/Longitude points

The JavaScript code works just fine in Ruby:

lat1 = 0.9250245 # starting point's latitude (in radians)
lon1 = 0.0174532 # starting point's longitude (in radians)
brng = 1.67551   # bearing (in radians)
d    = 124.8     # distance to travel in km
R    = 6371.0    # earth's radius in km

lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
          Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) )
# => 0.9227260710962849                  

lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                 Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2))
# => 0.0497295729068199      

# NB. Ensure that all values are cast to floats    

Upvotes: 8

Related Questions