package com.javaweb.geo.utils;
|
|
import com.javaweb.common.utils.NumberUtils;
|
import org.gavaghan.geodesy.Ellipsoid;
|
import org.gavaghan.geodesy.GeodeticCalculator;
|
import org.gavaghan.geodesy.GeodeticCurve;
|
import org.gavaghan.geodesy.GlobalCoordinates;
|
|
public class ExceptionDistanceUtils {
|
|
public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid)
|
{
|
//创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
|
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
|
|
return geoCurve.getEllipsoidalDistance();
|
}
|
|
/**
|
* 计算距离
|
* @param lng1
|
* @param lat1
|
* @param lng2
|
* @param lat2
|
* @return
|
*/
|
public static double distance(Double lng1,Double lat1 ,Double lng2 ,Double lat2){
|
GlobalCoordinates source = new GlobalCoordinates(lat1, lng1);
|
GlobalCoordinates target = new GlobalCoordinates(lat2, lng2);
|
|
double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
|
return NumberUtils.div(meter2,1,2);
|
}
|
|
public static void main(String[] args) {
|
double distance = distance(116.510842,39.90777,116.510958,39.90786);
|
System.out.println(distance);
|
}
|
|
}
|