자바 2 개의 경위도 사이 의 거 리 를 계산 합 니 다.

/**
 *   2         
 */
public class GPSUtil {
    //android     2         ,            
    public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
        float[] results = new float[1];
        Location.distanceBetween(lat1, lon1, lat2, lon2, results);
        return results[0];
    }

    /**
     * DecimalFormat     
     * double    2   
     */
    public static double fun2(double f) {
        DecimalFormat df = new DecimalFormat("#.00");
        return Double.parseDouble(df.format(f));
    }

    //           
    private static final double EARTH_RADIUS = 6378137.0;//6378137.0; 6378.137

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * java   2                    2             ,           ,                
     * @param lat1        
     * @param lng1        
     * @param lat2        
     * @param lng2        
     * @return
     */
    public static double GetDistance( double lat1,double lng1,  double lat2,double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    public static double GetDistance1(double lat1, double long1, double lat2, double long2) {
        double a, b, d, sa2, sb2;
        lat1 = rad(lat1);
        lat2 = rad(lat2);
        a = lat1 - lat2;
        b = rad(long1 - long2);

        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
        return d;
    }
}

좋은 웹페이지 즐겨찾기