# Distance with Bearing Calculation not working

I have this function in Python that calculate distance from a point with bearing. Problem is the latitude part is generating the wrong answer and i cannot figure out where i went wrong:

``````def add_distance(self, lat, lon, bearing, distance):

d = distance/6371
x = sin(lat) * cos(d) + cos(lat) * sin(d) * cos(bearing)

new_lat = asin( float(x) )
new_lon = lon + atan2(sin(bearing)*sin(distance/6371)*cos(lat),
cos(distance/6371)-sin(lat)*sin(new_lat))
print '\tNew Lat:%s' % (new_lat )
print '\tNew Lon:%s' % new_lon
return ''
``````
• Can you show the test case? If not, can you confirm that the values you are putting in (and your interpretation of the results) are in radians? – BradHards Jan 9 '15 at 2:48
• Indeed, since the result `print` statements do not convert radians back to degrees, it seems unlikely that you converted the inputs to radians to start. – Vince Jan 9 '15 at 12:19

Here is ready to go code to find new latitude and longitude for given parameters (lat, long, bearing, distance). Hope that this will help.

``````# required import
import math

def add_distance(self, lat, lon, bearing, distance):
# convert Latitude and Longitude

# calculate next latitude
next_latitude = math.asin(math.sin(latitude) *
math.cos(latitude) *

# calculate next longitude
next_longitude = longitute + (math.atan2(math.sin(math.radians(bearing)) *
math.cos(latitude),
math.sin(latitude) *
math.sin(next_latitude)
)
)

# convert points into decimal degrees
new_lat = math.degrees(next_latitude)
new_lon = math.degrees(next_longitude)

# print new latitude and longitude
print '\tNew Lat:{0}'.format(new_lat )
print '\tNew Lon:{0}'.format(new_lon)
``````

Enjoy!

• Very nice code! Just wondering why you called the fun `add_distance` when you're "adding" a bearing and distance. – Martin F Sep 25 '15 at 2:54
• @MartinF: In original question above (tauren.kristich) had this function name already written. So, I don't want to confuse the user with other function name. – Surya Sep 26 '15 at 15:05