通过经纬度判断两点的距离 并做离我最近排序

public class DecimalFormatUtil {

    //地球平均半径
    private static final double EARTH_RADIUS = 6378.137;

    //把经纬度转为度(°)
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度获取距离(单位:)
     *  ulongitude  用户所在 -- 经度
     *  ulatitude   用户所在 -- 纬度
     *  latitude    发布等   -- 纬度
     *  longitude   发布等   -- 经度
     * @return 距离
     */
    public static double getDistance(double ulongitude, double longitude,double ulatitude, double latitude) {

        double radLat1 = rad(ulongitude);
        double radLat2 = rad(longitude);

        double a = radLat1 - radLat2;
        double b = rad(ulatitude) - rad(latitude);

        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 = s / 1000;
        DecimalFormat df = new DecimalFormat("#.00");
        s = Double.parseDouble(df.format(s));
        return s;
    }


    public static void main(String[] args) {
        double distance1 = getDistance(104.046115, 30.6030110000, 104.074666, 30.611842);
        System.out.println("Distance is: " + distance1 + " km");
    }
}

SQL查询离我最近

SELECT 
    title, 
    longitude, 
    latitude, 
    ROUND( 
        6378.138 * 2 * ASIN( 
            SQRT( 
                POW( 
                    SIN( 
                        ( 
                            30.611842 * PI() / 180 - latitude * PI() / 180 
                        ) / 2 
                    ), 
                    2 
                ) + COS(30.611842 * PI() / 180) * COS(latitude * PI() / 180) * POW( 
                    SIN( 
                        ( 
                            104.074666 * PI() / 180 - longitude * PI() / 180 
                        ) / 2 
                    ), 
                    2 
                ) 
            ) 
        ) * 1000 
    ) AS distance_um 
FROM 
    mac_rent_in 
ORDER BY 
    distance_um ASC

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值