I recently used the background data query function for andorid. One requirement is to simulate the nearby user search function. The database stores the longitude and latitude information and user information of each user, and queries the user information within a user's radius of N kilometers through the longitude and latitude information transmitted by the current user.
Database Table Structure
Table Information
Table Name |
Mobile_User |
Mu_id |
Auto-increment, primary key |
Mu_u_id |
User table ID foreign key |
Mu_longitud |
Precision |
Mu_latitude |
Latitude |
(For other information, we can list four fields here)
First, a method is required to spread the passed longitude and latitude according to the radius of N kilometers to find the upper and lower latitude and longitude values N kilometers away from the center latitude and longitude. Effect
Do not spray
When the longitude and latitude are generated in the center, the accuracy at the top and bottom is unchanged, and only the latitude changes. The principle is the same when the left and right sides are generated. Only the accuracy changes, and the latitude remains unchanged.
Therefore, you only need to generate the upper and lower latitudes to ensure the accuracy of the left and right sides.
According to the article on the internet, http://digdeeply.info/archives/06152067.html this article is achieved by using PHP longitude and latitude query. The Code modified to java is as follows:
[Java] view plaincopy
/**
* Generate a quartile longitude and latitude centered on the center.
*
* @ Param lat latitude
* @ Param lon precision
* @ Param raidus radius (in meters)
* @ Return
*/
Public static double [] getAround (double lat, double lon, int raidus ){
Double latitude = lat;
Double longpolling = lon;
Double degree = (24901*1609)/360.0;
Double raidusMile = raidus;
Double dpmLat = 1/degree;
Double radiusLat = dpmLat * raidusMile;
Double minLat = latitude-radiusLat;
Double maxLat = latitude + radiusLat;
Double mpdLng = degree * Math. cos (latitude * (Math. PI/180 ));
Double dpmLng = 1/mpdLng;
Double radiusLng = dpmLng * raidusMile;
Double minLng = longbench-radiusLng;
Double maxLng = longbench + radiusLng;
Return new double [] {minLat, minLng, maxLat, maxLng };
}
In this way, the longitude and latitude of the four sides have been generated.
The next step is to query the data that matches the longitude and latitude of the database. If the data volume is large, it will consume a lot of time and traffic. Therefore, paging query is required.
The Code is as follows:
[SQL] view plaincopy
Select * from mobile_user
Where mu_latitude <> 0
And mu_longitud> # left_lat #
And mu_longitud <# right_lat #
And mu_latitude> # down_lon #
And mu_latitude <# top_lon #
And mu_u_id <> # uid #
Order by ACOS (SIN (# lat # * 3.1415)/180) * SIN (mu_latitude * 3.1415)/180)
+ COS (# lat # * 3.1415)/180) * COS (mu_latitude * 3.1415)/180)
* COS (# lon # * 3.1415)/180-(mu_longitud * 3.1415)/180 ))
* 6380 asc limit # start #, # end #
I am using the ibatis framework. In SQL, the parameters I passed start and end. The SQL statement calculates the distance between each piece of data and the longitude and latitude of the center and sorts the data in the nearest order. The SQL statement evolved from the following method:
The method is to calculate the linear distance between two longitude and latitude.
[Java] view plaincopy
/**
* Distance between the longitude and latitude of the computing center and the target longitude and latitude (meters)
*
* @ Param centerLon
* Center precision
* @ Param centerLan
* Central latitude
* @ Param targetLon
* Accuracy to be calculated
* @ Param targetLan
* Latitude to be calculated
* @ Return Meter
*/
Private static double distance (double centerLon, double centerLat, double targetLon, double targetLat ){
Double jl_jd = 102834.74258026089786013677476285; // The unit of longitude is meter;
Double jl_wd = 111712.69150641055729984301412873; // The unit is meters per latitude;
Double B = Math. abs (centerLat-targetLat) * jl_jd );
Double a = Math. abs (centerLon-targetLon) * jl_wd );
Return Math. sqrt (a * a + B * B ));
}
This not only achieves paging processing, but also achieves the straight line distance between the longitude and latitude of each piece of data and the central longitude and latitude (in meters ).
Finally, a json array is formed and returned to android.
Please take a note.