基于GPS与经纬度距离计算

2021-04-23 11:28:30 浏览数 (1)

基于GPS与经纬度距离计算

代码语言:javascript复制
# -*- coding:utf-8 -*-
# /usr/bin/python

import warnings
warnings.filterwarnings("ignore")
from math import radians, cos, sin, asin, sqrt

# 方法1
def geodistance(lat1,lng1,lat2,lng2):
    ''''数学转换'''
    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/1000,3)
    return distance

# 方法2
from geopy.distance import geodesic

def fromGeodesic(lat1,lng1,lat2,lng2):
    '''调用包'''
    distance=geodesic((lat1,lng1), (lat2,lng2))
    return distance

# 方法3
def simpleDistance(lat1,lng1,lat2,lng2):
    dx = lng1 - lng2 # 经度差值
    dy = lat1 - lat2 # 纬度差值
    b = (lat1   lat2) / 2.0 # 平均纬度
    Lx = radians(dx) * 6367.0 * cos(radians(b))# 东西距离
    Ly = 6367.0 * radians(dy) #南北距离
    distance = sqrt(Lx * Lx   Ly * Ly)
    return distance



print(geodistance(39.941,116.45,39.94,116.451))
print(fromGeodesic(39.941,116.45,39.94,116.451))
print(simpleDistance(39.941,116.45,39.94,116.451))
gps

0 人点赞