Python Forum

Full Version: calculate distance based on gps coordinates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi i do have a homework that it needs to calculate the distance between two points on earth. the earth's radius is R = 6372.8Km.
so i have to ask the user to input the two coordinates and based on haversine's formula to creae the code. here i have:
from math import sin, cos, sqrt, atan2, radians

# approximate radius of earth in km
R = 6372.8


x1=int(input('Please state the of the longitude of point A: '))
y1=int(input('Please state the of the latitude of point A: '))

x2=int(input('Please state the of the longitude of point B: '))
y2=int(input('Please state the of the latitude of point B: '))

lat1 = radians(x1)
lon1 = radians(y1)
lat2 = radians(x2)
lon2 = radians(y2)

dlon = lon2 - lon1
dlat = lat2 - lat1

a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
print("Result:", distance,"Km")
if i give an integer number everything works fine. But longtitude and latitude are floats so if i give floats it comes with an error:
Error:
x1=int(input('Please state the of the longitude of point A: ')) ValueError: invalid literal for int() with base 10: '98.5'
Any ideas please?
Use float() instead of int() maybe?