Calculate the distance between two places based on latitude and longitude in Oracle
function to get radians:
CREATE OR REPLACE FUNCTION radian (d number) return number
Is
PI number: = 3.141592625;
Begin
return d* pi/180.0;
End;
Test this function:
Select Radian (360) from dual;
A function for calculating distances according to latitude and longitude:
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 (A/2), 2) +
COS (RADLAT1) * cos (RADLAT2) * Power (sin (B/2), 2));
S: = S * earth_padius;
S: = Round (S * 10000)/10000;
return s;
End
example, calculate the distance according to the city name:
The latitude and longitude of the Oriental Pearl Tower is: The latitude and longitude of the 121.506656,31.245087 subway station is: 121.508883,31.243481
Sql> Select Getdistance (' 121.506656 ', ' 31.245087 ', ' 121.508883 ', ' 31.243481 ') from dual;
The output is 0.2649 kilometers.