Oracle gets the distance between any 2 longitude and latitude

Source: Internet
Author: User

When developing android lbs positioning, you can get a point (longitude and latitude) that requires businesses to be located one kilometer nearby. All these businesses are in the local database and are marked with longitude and latitude, that is to obtain the distance between this point and the longitude and latitude in the database. If the distance is less than 1 kilometer, it will be retrieved;

Write two functions first:
1. Obtain the radian function.

SQL code
Create or replace function rad (d number) RETURN NUMBER
Is
PI number: = 3.141592625;
 
Begin
Return d * PI/180.0;
End;
 
Select RAD (360) from dual;


2. Calculate the Distance Based on the longitude and latitude.
SQL code
Create or replace function GetDistance (lat1 number,
Lng1 number,
Lat2 number,
Lng2 number) return number is
Earth_padius number: = 6378.137;
RadLat1 number: = rad (lat1 );
RadLat2 number: = rad (lat2 );
A number: = radLat1-radLat2;
B number: = rad (lng1)-rad (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)/10000;
Return s;
 
End;


Obtain the longitude and latitude Based on the Baidu pick-up coordinates:
Http://dev.baidu.com/wiki/static/map/API/tool/getPoint/

The longitude and latitude of Oriental Pearl Tower are: 121.506656, 31.245087
The longitude and latitude of Lujiazui subway station are: 121.508883, 31.243481

The distance obtained from these two points is kilometers!
SQL code
Select GetDistance ('192. 100', '31. 100', '192. 100', '31. 100') from dual;
 
Result: 0.2649 km

Author: "Liu Yafei"
 

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.