Python---计算经纬度之间的距离

计算经纬度之间的距离

from math import radians,sin,cos,asin,sqrt
def haversine(s1, s2): # 经度1,纬度1,经度2,纬度2 (十进制度数)
        """
        Calculate the great circle distance between two points 
        on the earth (specified in decimal degrees)
        """
        # 将十进制度数转化为弧度
        lon1, lat1, lon2, lat2 = map(radians, [s1[0], s1[1], s2[0], s2[1]])
        
        # haversine公式
        dlon = lon2 - lon1 
        dlat = lat2 - lat1 
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * asin(sqrt(a)) 
        r = 6371 # 地球平均半径,单位为公里
        return c * r # 返回单位为 km
  
  station1 = [116.35, 40.05]
  station2 = [116.339,39.929]
  haversine(station1,station2) # 13.487182564909839

你可能感兴趣的:(Python,python)