# 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;
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 Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

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

## Alibaba Cloud Free Trial

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