专业编程基础技术教程

网站首页 > 基础教程 正文

Python计算两坐标之间的距离

ccvgpt 2024-08-16 14:58:02 基础教程 10 ℃

函数如下:


Python计算两坐标之间的距离

import math

#计算两坐标之间的距离

def get_distance(lat_1, lng_1, lat_2, lng_2):

d_lat = lat_2 - lat_1

d_lng = lng_2 - lng_1

temp = (

math.sin(d_lat / 2) ** 2

+ math.cos(lat_1)

* math.cos(lat_2)

* math.sin(d_lng / 2) ** 2

)

return 6373.0 * (2 * math.atan2(math.sqrt(temp), math.sqrt(1 - temp)))


直接引用函数即可计算出结果:

a=get_distance(114.12618,31.577694432118374,114.12618,31.541721567881627)

输出结果a=118.24717646683116

更多学习资料请关注公众号“读书同友会”

Tags:

最近发表
标签列表