Skip to content

Instantly share code, notes, and snippets.

@FernandoEscher
Created July 11, 2013 23:57
Show Gist options
  • Save FernandoEscher/5980331 to your computer and use it in GitHub Desktop.
Save FernandoEscher/5980331 to your computer and use it in GitHub Desktop.
def point_at_distance(center, distance, bearing=180)
pi = Math::PI
r_e = 6378137.0 #meters
d = distance/r_e
lat1 = (pi/180) * center.latitude
lng1 = (pi/180) * center.longitude
tc = (pi/180) * bearing
y = Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc))
dlng = Math.atan2(
Math.sin(tc)*Math.sin(d)*Math.cos(lat1),
Math.cos(d) - Math.sin(lat1)*Math.sin(y)
)
x = ((lng1-dlng+pi) % (2*pi)) - pi
lat = y * (180/pi)
lng = x * (180/pi)
center.factory.point(lat,lng)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment