#!/usr/bin/env python # encoding: utf-8 # Author: GuoCheng # 2022/4/22 10:44 from math import asin, sqrt, cos, sin, radians def geo_distance(lng1, lat1, lng2, lat2): # lng1,lat1,lng2,lat2 = (120.12802999999997,30.28708,115.86572000000001,28.7427) lng1, lat1, lng2, lat2 = map(radians, [float(lng1), float(lat1), float(lng2), float(lat2)]) # 经纬度转换成弧度 dlon = lng2 - lng1 dlat = lat2 - lat1 a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2 distance = 2 * asin(sqrt(a)) * 6371 * 1000 # 地球平均半径,6371km # 单位:米 distance = round(distance, 3) return distance s = geo_distance(116.304665600000,39.976920900000,116.305097538773,39.982684462583) print(s)