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"