java,mysql-500 meters of nearby locations based on a given latitude and longitude point-rational use of the algorithm

Source: Internet
Author: User
Tags asin cos sin

LBS Spherical Distance formula

Http://wiki.myoa.info/zh-blog:20

java,mysql-500 meters of nearby locations based on a given latitude and longitude point-rational use of the algorithm

Recently do a project: you need to query a site (known to the site latitude and longitude) 500 meters in the range of other sites. So, the first thing I think about is, for each record, go to traverse, with each point in the database distance calculation, when the distance is less than 500 meters, the match is considered. Doing so can actually get results, but the efficiency is extremely low, because each record is going to loop to match n data, the time it consumes is conceivable.

So I think of a first filter out the latitude and longitude of the range before the calculation. For example, the square of four points, so I search on the Internet, unexpectedly, query to a about this calculation near the location of the search, the use of Python implementation of this idea. Therefore, the reference to the original text in the algorithm, the use of Java implementation.

The implementation principle is also very similar, first calculate the point around the four points of the rectangle, and then use latitude and longitude to directly match the records in the database.

Idea: The coordinate range of the range 500 meters near the given coordinates is calculated first. Although it is a circle, we can first find out the circle's external square, and then take the square latitude and longitude range to search the database.

First come to the bounds of the boundary on the sides of the thing. In the Haversin formula, φ1 =φ2 can be

Then the boundary between the north and south sides of the range, in the Haversin formula to make δλ= 0, can be

⊿φ= d/r

Written in Java code is

/Calculate the latitude and longitude range of the query point <span style= "font-family: Microsoft Jacob Black, Helvetica, Times, Arial, serif;" >lat known latitude, lng known longitude </span>
Double R = 6371.137;//r is the radius of the earth, desirable mean 6371.137km
Double dis = 0.5;//distance 0.5 km
Double dlng = 2*math.asin (Math.sin (dis/(2*r))/math.cos (lat*math.pi/180)); ⊿λ the boundary of the side of the thing
DLNG = dlng*180/math.pi;//Angle converted to radians
Double Dlat = dis/r; ⊿φ range boundary between north and south sides
Dlat = Dlat*180/math.pi;

Finally, you can draw the coordinates of four points:
Left-top: (Lat + dlat, lng–dlng)
Right-top: (Lat + dlat, LNG + dlng)
Left-bottom: (Lat–dlat, LNG–DLNG)
Right-bottom: (Lat–dlat, LNG + dlng)
This is how you filter queries.

Public list<property> findneighposition (double longitude,double latitude) {
Calculate the latitude and longitude range of a query point first
Double r = 6371;//Earth radius km
Double dis = 0.5;//0.5 km distance
Double dlng = 2*math.asin (Math.sin (dis/(2*r))/math.cos (latitude*math.pi/180));
DLNG = dlng*180/math.pi;//Angle converted to radians
Double Dlat = dis/r;
Dlat = Dlat*180/math.pi;
Double Minlat =latitude-dlat;
Double Maxlat = Latitude+dlat;
Double minlng = longitude-dlng;
Double maxlng = longitude + dlng;

String hql = "From property where longitude>=?" and longitude =<? and latitude>=? latitude=<? and state=0 ";
Object[] values = {Minlng,maxlng,minlat,maxlat};

list<property> list = find (hql, values);
return list;
}

Source: http://www.cnblogs.com/zt007/p/6373722.html

java,mysql-500 meters of nearby locations based on a given latitude and longitude point-rational use of the algorithm

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.