Python Forum
calculate distance based on gps coordinates
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculate distance based on gps coordinates
#1
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?
Reply
#2
Use float() instead of int() maybe?
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Distance between indicies of a list johnissa 2 3,040 Apr-25-2018, 01:04 AM
Last Post: johnissa
  Distance between two points! zepel 14 33,807 Apr-28-2017, 08:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020