Calculates the distance between two places in Oracle based on latitude and longitude ____oracle

Source: Internet
Author: User
Tags cos

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.