Created
June 13, 2018 16:08
-
-
Save ImSingee/e6f561410ecb70071e7935fd4cc6fe32 to your computer and use it in GitHub Desktop.
计算经纬度距离差
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_distance(latitude_start, latitude_end, longitude_start, longitude_end): | |
""" | |
计算经纬度距离差 | |
:param latitude_start: 纬度1 | |
:param latitude_end: 纬度2 | |
:param longitude_start: 经度1 | |
:param longitude_end: 经度2 | |
:return: | |
""" | |
import math | |
# 角度转弧度 | |
rad_latitude_start = math.radians(latitude_start) | |
rad_latitude_end = math.radians(latitude_end) | |
rad_longitude_start = math.radians(longitude_start) | |
rad_longitude_end = math.radians(longitude_end) | |
# 求弧度差 | |
d_latitude = rad_latitude_start - rad_latitude_end | |
d_longitude = rad_longitude_start - rad_longitude_end | |
# 求距离 | |
distance = 2 * 6378.137 * math.asin(math.sqrt( | |
math.pow(math.sin(d_latitude / 2), 2) + | |
math.cos(rad_latitude_start) * math.cos(rad_latitude_end) * math.pow(math.sin(d_longitude / 2), 2) | |
)) | |
return distance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment