歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 根據經緯度分別用Java和Oracle存儲過程計算兩點距離

根據經緯度分別用Java和Oracle存儲過程計算兩點距離

日期:2017/3/1 10:05:59   编辑:Linux編程

在給定2點的經緯度,通過java代碼和Oracle存儲過程來計算出點的距離 單位是(米)

oracle存儲過程:

create or replace procedure SP_GET_DISTANCE
(cx in number,cy in number,sx in number, sy in number,distance out varchar2)
is
d number;
x number;
y number;
r number;
pi number;
begin
--開始計算
r:=6371229;--地球半徑
pi:=3.14159265358979323;--圓周率
x:=(sx-cx)*pi*r*cos((sy+cy)/2*pi/180)/180;
y:=(sy-cy)*pi*r/180;
d:=SQRT(power(x,2)+power(y,2));
distance:=to_char(d,9999999999999.99);
end SP_GET_DISTANCE;

java代碼:

package com.wpn.web.util;

public class Distance {
private final static double PI = 3.14159265358979323;// 圓周率
private final static double R = 6371229; // 地球的半徑

private Distance() {
}

/**
* 緯度lat 經度lon
* @param longt1
* @param lat1
* @param longt2
* @param lat2
* @return
*/
public static 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 enum GaussSphere {
Beijing54, Xian80, WGS84,
}

private static double Rad(double d) {
return d * Math.PI / 180.0;
}
public static double DistanceOfTwoPoints(double lng1, double lat1, double lng2, double lat2, GaussSphere gs) {
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 * (gs == GaussSphere.WGS84 ? 6378137.0 : (gs == GaussSphere.Xian80 ? 6378140.0 : 6378245.0));
s = Math.round(s * 10000) / 10000;
return s;
}*/

public static void main(String[] arg){
double longt1 = 116.515502;
double lat1 = 39.863898;
double longt2 = 116.304187;
double lat2 = 40.052584;
System.out.println(getDistance(longt1,lat1,longt2,lat2));
}
}

Copyright © Linux教程網 All Rights Reserved