java mysql根据经纬度获取距离

DELIMITER $$
DROP  FUNCTION IF  EXISTS `fun_distance`$$
CREATE FUNCTION `fun_distance`(lat1 float,lng1 float,lat2 float,lng2 float) RETURNS float
BEGIN
set @num=6371.229*2*ASIN(SQRT(POW(SIN((lat1*PI()/180-lat2*PI()/180)/2),2)+COS(lat1*PI()/180)*COS(lat2*PI()/180)*POW(SIN((lng1*PI()/180-lng2*PI()/180)/2),2)))*1000;
RETURN @num;
END$$
DELIMITER ;


select fun_distance(29.490295,97.486654,29.615467,106.581515) 


java获取

public class Map2 {

private final double PI = 3.14159265358979323; //圆周率
    private final double R = 6371229;              //地球的半径
   
   
    public double getDistance(double longt1, double lat1, double longt2, double lat2){
        double x,y,distance;
        x=(longt2-longt1)*PI*R*Math.cos( ((lat1+lat2)/2) *PI/180)/180;
        y=(lat2-lat1)*PI*R/180;
        distance=Math.hypot(x,y);
        return distance;
    }
   
   
    public double getLongt(double longt1, double lat1, double distance){
        double a = (180*distance)/(PI*R*Math.cos(lat1*PI/180));
        return a;
    }
   
   
    public double getLat(double longt1, double lat1, double distance){
        double a = (180*distance)/(PI*R*Math.cos(lat1*PI/180));
        return a;
    }
   
    public static void main(String[] args){
        Map2 m = new Map2();
        double s = m.getDistance(107.486654,29.490295, 106.581515,29.615467);
        System.out.println(s);
       
        double longt = m.getLongt(112.0235, 23.2563, 10227.5985);
        System.out.println(longt);
       
        double lat = m.getLat(112.0235, 23.2563, 11131.9859);
//        System.out.println(lat);
       
    }
}


你可能感兴趣的:(java,数据库)