一次程序测试过程中发现之前的Oracle存储过程计算地图两个坐标点之间的距离有误差,之后在百度中完善更新了一下,具体过程如下:
这是之前的函数,有误差
CREATE OR REPLACE FUNCTION GetDistance(lat1 number,
lng1 number,
lat2 number,
lng2 number) RETURN NUMBER is
earth_padius number := 6378.137;
radLat1 number := Radian(lat1);
radLat2 number := Radian(lat2);
a number := radLat1 - radLat2;
b number := Radian(lng1) - Radian(lng2);
s number := 0;
begin
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 * 10000) / 10;
return s;
end;
后来在网上找的一个存储过程:
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));
}
}
摘自https://www.cnblogs.com/feizhuliu/archive/2012/12/07/6312112.html
把Oracle存储过程改为我所需要的函数:
CREATE OR REPLACE FUNCTION GetDistance_new(cx number,
cy number,
sx number,
sy number) RETURN NUMBER is
r number;
pi number;
x number;
y number;
d number;
s 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));
s := Round(d, 2);
return s;
end;
完成。
分享一个测试两个地理坐标直线距离的百度API:http://developer.baidu.com/map/jsdemo.htm#a6_1
高德地图测试两个地理坐标直线距离:
http://lbs.amap.com/api/javascript-api/example/calcutation/calculate-distance-between-two-markers