Home > Backend Development > Python Tutorial > python根据经纬度计算距离示例

python根据经纬度计算距离示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-16 08:45:17
Original
2593 people have browsed it

复制代码 代码如下:

/**
 * 计算两点之间距离
 * @param _lat1 - start纬度
 * @param _lon1 - start经度
 * @param _lat2 - end纬度
 * @param _lon2 - end经度
 * @return km(四舍五入)
 */
public static double getDistance(double _lat1,double _lon1, double _lat2,double _lon2){
 double lat1 = (Math.PI/180)*_lat1;
 double lat2 = (Math.PI/180)*_lat2;

 double lon1 = (Math.PI/180)*_lon1;
 double lon2 = (Math.PI/180)*_lon2;

 //地球半径
 double R = 6378.1;

 double d =  Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;

 return new BigDecimal(d).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
}

public static void main(String[] args) {
 System.out.println(getDistance(45.73990, 126.55893,45.73876, 126.55037));
}

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template