代码借自:https://blog.csdn.net/zhuqiuhui/article/details/53180395
夹角示意图说明:
import math
# 两个经纬度之间的距离
def LatLng2Dist(LatZero,LngZero,Lat,Lng):
ra = 6378140 # radius of equator: meter
rb = 6356755 # radius of polar: meter
flatten = (ra - rb) / ra # Partial rate of the earth
# change angle to radians
radLatA = math.radians(LatZero)
radLonA = math.radians(LngZero)
radLatB = math.radians(Lat)
radLonB = math.radians(Lng)
pA = math.atan(rb / ra * math.tan(radLatA))
pB = math.atan(rb / ra * math.tan(radLatB))
x = math.acos(math.sin(pA) * math.sin(pB) + math.cos(pA) * math.cos(pB) * math.cos(radLonA - radLonB))
c1 = (math.sin(x) - x) * (math.sin(pA) + math.sin(pB))**2 / math.cos(x / 2)**2
c2 = (math.sin(x) + x) * (math.sin(pA) - math.sin(pB))**2 / math.sin(x / 2)**2
dr = flatten / 8 * (c1 - c2)
distance = ra * (x + dr)
return distance
# 点1到点2方向沿逆时针方向转到正北方向的夹角
def LatLng2Degree(LatZero,LngZero,Lat,Lng):
"""
Args:
point p1(latA, lonA)
point p2(latB, lonB)
Returns:
bearing between the two GPS points,
default: the basis of heading direction is north
"""
radLatA = math.radians(LatZero)
radLonA = math.radians(LngZero)
radLatB = math.radians(Lat)
radLonB = math.radians(Lng)
dLon = radLonB - radLonA
y = math.sin(dLon) * math.cos(radLatB)
x = math.cos(radLatA) * math.sin(radLatB) - math.sin(radLatA) * math.cos(radLatB) * math.cos(dLon)
brng = math.degrees(math.atan2(y, x))
brng = (brng + 360) % 360
return brng
if __name__ == "__main__":
LatLng2Degree(40,116,40,117)