计算两个坐标之间的距离 Python

Calculate the distance between two coordinates with Python

我有一张地图,他们在其中找到了几个点 (lat/long),想知道它们之间的距离。

那么,给定一组 lat/long 坐标,我如何计算它们在 python 中的距离?

我曾经写过 this answer. It details the use of the Haversine formula 的 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)))

确保传递给函数的坐标以弧度为单位。如果是度数,可以先换算一下:

lng_1, lat_1, lng_2, lat_2 = map(math.radians, [lng_1, lat_1, lng_2, lat_2])