计算两个经纬度之间的距离(java和数据库实现)

java实现:

package com.example.util;

public class LocationUtils {
	/**
     * 赤道半径
     */
    private static double EARTH_RADIUS =6378.137;

    private static double rad(double d) {
        return d * 3.141592625 / 180.0;
    }
    
    /**
     * 
     * Description : 通过经纬度获取距离(单位:米)
     * @param origin      出发点
     * @param destination 目的地
     * @return double
     */
    public static double getDistance(String origin, String destination) {
        if (origin == null) {
        	System.out.println("出发点 经纬度不可以为空!");
            return 0;
        }
        if (destination == null) {
        	System.out.println("目的地 经纬度不可以为空!");
            return 0;
        }
        String[] temp = origin.split(",");
        String[] temp2 = destination.split(",");

        double radLat1 = rad(Double.parseDouble(temp[1]));
        double radLat2 = rad(Double.parseDouble(temp2[1]));
        double a = radLat1 - radLat2;
        double b = rad(Double.parseDouble(temp[0])) - rad(Double.parseDouble(temp2[0]));
        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 * 100d) / 100d;
        //s = s * 1000;
        return s;
    }
    
    public static void main(String[] args) {
		String a="113.02606940291372,28.184938487226933";
		String b="112.99730206529058,28.252247339541047";
		
		double distance=getDistance(a,b);
		
		System.out.println("两点之间的距离"+distance+"KM");
		
	}
}

数据库函数SQL函数

create or replace function yz_cs.FUNC_SF_GET_DISTANCE(store_positiona varchar2,
                                             store_positionb varchar2)
  return number is
  --计算两个经纬度之间的距离
  earth_padius number := 6378.137;--地球半径
  lng1 number; --店铺A 位置的经度
  lat1 number; --店铺A 位置的纬度
  lng2 number; --店铺B 位置的经度
  lat2 number; --店铺B 位置的纬度
  radlat1 number; 
  radlat2 number; 
  a       number; 
  b       number; 
  s       number; 
begin
  begin
    if store_positiona is not null and instr(store_positiona,'undefined')=0 then
      --'x:113.02606940291372,y:28.184938487226933' 截取字符串的x 值  经度
      select to_number(substr(store_positiona,
                              instr(store_positiona, ':', 1, 1) + 1,
                              instr(store_positiona, ',', 1, 1) -
                              (instr(store_positiona, ':', 1, 1) + 1)))
        into lng1
        from dual;
     --截取 y 值 纬度
      select to_number(substr(store_positiona,
                              instr(store_positiona, ':', -1, 1) + 1))
        into lat1
        from dual;
    end if;
  end;
  begin
    if store_positionb is not null and instr(store_positionb,'undefined')=0 then
      --'x:113.02606940291372,y:28.184938487226933' 截取字符串的 x 值  经度
      select to_number(substr(store_positionb,
                              instr(store_positionb, ':', 1, 1) + 1,
                              instr(store_positionb, ',', 1, 1) -
                              (instr(store_positionb, ':', 1, 1) + 1)))
        into lng2
        from dual;
     --截取 y 值 纬度
      select to_number(substr(store_positionb,
                              instr(store_positionb, ':', -1, 1) + 1))
        into lat2
        from dual;
    end if;
  end;

  begin
    --计算经纬度公式:点A的经 纬度为(LonA, LatA) 点B 的经纬度为(LonB, LatB)
    --C = sin(LatA)*sin(LatB) + cos(LatA)*cos(LatB)*cos(MLonA-MLonB)
    --Distance = R*Arccos(C)*Pi/180
    s := 200;--默认返回 200
    if lat1 is not null and lng1 is not null and lat2 is not null and lng2 is not null then 
        radlat1 := (lat1*3.141592625)/180.0;
        radlat2 := (lat2*3.141592625)/180.0;
        a := radlat1-radlat2;
        b := (lng1*3.141592625)/180.0 - (lng2*3.141592625)/180.0;
        
        s := 2 *
           asin(sqrt(power(sin(a / 2), 2) +
                     cos(radlat1) * cos(radlat2) * power(sin(b / 2), 2)));
        s := s * earth_padius;
        s := round(s * 100) / 100;
        end if;
  end;
  return s;
end;

注:数据库经纬度字段形式('x:113.02606940291372,y:28.184938487226933');

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值