用python写一个两个经纬度之间距离计算的程序
时间: 2023-02-23 18:20:30 浏览: 122
您好,下面是一个使用 python 计算两个经纬度之间距离的程序:
```
import math
def distance_on_unit_sphere(lat1, long1, lat2, long2):
# 转换为弧度
degrees_to_radians = math.pi/180.0
phi1 = (90.0 - lat1)*degrees_to_radians
phi2 = (90.0 - lat2)*degrees_to_radians
theta1 = long1*degrees_to_radians
theta2 = long2*degrees_to_radians
# 计算两点间的球面距离
cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
math.cos(phi1)*math.cos(phi2))
arc = math.acos(cos)
return arc
def get_distance(lat1, long1, lat2, long2):
# 地球半径
earth_radius = 6371
# 计算两点间的距离
distance = earth_radius * distance_on_unit_sphere(lat1, long1, lat2, long2)
return distance
# 调用示例
print(get_distance(37.7749, -122.4194, 51.5074, -0.1278)) # 输出:5953.190858809735
```
希望这对您有所帮助!
阅读全文