Method: Geocoder::Calculations#endpoint

Defined in:
lib/geocoder/calculations.rb

#endpoint(start, heading, distance, options = {}) ⇒ Object

Given a start point, heading (in degrees), and distance, provides an endpoint. The starting point is given in the same way that points are given to all Geocoder methods that accept points as arguments. It can be:

  • an array of coordinates ([lat,lon])

  • a geocodable address (string)

  • a geocoded object (one which implements a to_coordinates method which returns a [lat,lon] array



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/geocoder/calculations.rb', line 269

def endpoint(start, heading, distance, options = {})
  radius = earth_radius(options[:units])

  start = extract_coordinates(start)

  # convert degrees to radians
  start = to_radians(start)

  lat = start[0]
  lon = start[1]
  heading = to_radians(heading)
  distance = distance.to_f

  end_lat = Math.asin(Math.sin(lat)*Math.cos(distance/radius) +
                Math.cos(lat)*Math.sin(distance/radius)*Math.cos(heading))

  end_lon = lon+Math.atan2(Math.sin(heading)*Math.sin(distance/radius)*Math.cos(lat),
                Math.cos(distance/radius)-Math.sin(lat)*Math.sin(end_lat))

  to_degrees [end_lat, end_lon]
end