Java、MySql计算两坐标点的距离

Java计算两坐标点的距离:

/**
* 计算两经纬度点之间的距离(单位:米)
* 
* @param longitude1
*            坐标1经度
* @param latitude1
*            坐标1纬度
* @param longitude2
*            坐标2经度
* @param latitude2
*            坐标1纬度
* @return
*/
private static double countDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
double radLat1 = Math.toRadians(latitude1);
double radLat2 = Math.toRadians(latitude2);
double a = radLat1 - radLat2;
double b = Math.toRadians(longitude1) - Math.toRadians(longitude2);
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 * 6378137.0;
s = Math.round(s * 10000) / 10000;
return s;
}

MySql计算两坐标点距离(单位:米):

SELECT 
  (2 * ATAN2(SQRT(SIN((
    a.latitude - #{latitude}) * PI() / 180 / 2) * SIN((a.latitude - #{latitude}) * PI() / 180 / 2) + COS(#{latitude} * PI() / 180) * COS(a.latitude * PI() / 180) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2)
  ),
  SQRT(
    1- SIN(
      (
        a.latitude - #{latitude}) * PI() / 180 / 2) * SIN((a.latitude - #{latitude}) * PI() / 180 / 2) + COS(#{latitude} * PI() / 180) * COS(a.latitude * PI() / 180) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2) * SIN((a.longitude - #{longitude}) * PI() / 180 / 2)
      )
    )
  ) * 6378100 AS distance FROM 表 a 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值