Skip to content

Instantly share code, notes, and snippets.

@gzxultra
Created September 24, 2018 21:29
Show Gist options
  • Save gzxultra/9466d88bc716cbdf56ed548cab06c99a to your computer and use it in GitHub Desktop.
Save gzxultra/9466d88bc716cbdf56ed548cab06c99a to your computer and use it in GitHub Desktop.
calc gps distance
def calc_distance(lat1, lng1, lat2, lng2):
""" 城市内距离计算函数,这里是简化了 Haversine 公式,单位是米
在一个城市的范围内,可以近似认为经线和纬线是垂直的
"""
dx = lng1 - lng2 # 经度差值
dy = lat1 - lat2 # 纬度差值
b = (lat1 + lat2) / 2.0 # 平均纬度
lx = math.radians(dx) * 6367000.0 * math.cos(math.radians(b)) # 东西距离
ly = 6367000.0 * math.radians(dy) # 南北距离
return math.sqrt(lx * lx + ly * ly) # 用平面的矩形对角距离公式计算总距离
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment