Search for people near by latitude, and a simple implementation of distance sorting.

Source: Internet
Author: User
Tags abs cos
search for people near by latitude, and simple implementation by distance sorting

This is a simple implementation, the small amount of data can still meet the demand, write here to make a record. Of course, I hope there are other better options.

The main idea is: first, with their latitude and longitude as the center, the calculation of a certain radius of the square latitude and longitude boundary, and then use the square latitude and longitude boundary filter the user, and use a calculation of two points between the distance between the custom database function calculation distance, and then calculate the distance in reverse order calculate the square latitude and longitude in a certain radius

Long Raidus = 10000; Radius 10km double lat = 23.155778; Current Latitude Double lon = 113.262744;
Current longitude map<string, object> param = new hashmap<> ();
Param.put ("lat", lat);

Param.put ("Lon", Lon); private void Loadgeosquare (map<string, object> parm, double lat, double lon, long Raidus) {Double latitude = lat    
    ;    

    Double longitude = lon;    
    The equator circumference 24901 miles 1609 is the coefficient of conversion into a meter 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 = longitude-radiuslng;      
    Double maxlng = longitude + radiuslng;
    return new double[] {Minlat, minlng, Maxlat, maxlng}; Parm.put ("MinlAt ", Minlat);
    Parm.put ("Minlng", MINLNG);
    Parm.put ("Maxlat", Maxlat);
Parm.put ("Maxlng", MAXLNG); }
SQL Calculations

I use the mybatis, of course, SQL we should all be able to read

<select id= "Querynearbypage" resulttype= "Appusernearby" >
    select ID, Avatar, nickname, sex, Birthday, Lovecode,
    ROUND (Lat_lng_distance (#{lat}, #{lon}, lat, lon), 2 as distance from 
    zan_user zuser 
    WHERE 
    <! [cdata[and Lat > #{minlat} and Lat < #{maxlat} and Lon > #{minlng} and Lon < #{maxlng}]]> order by
    di Stance ASC 
    LIMIT #{rowstart},#{rowend}
</select>

The database function, which calculates the distance between two latitude and longitude, is also posted here:

CREATE FUNCTION ' lat_lng_distance ' (lat1 float, lng1 float, lat2 float, lng2 float)
RETURNS float
deterministic
  begin return
    6371 * 2 * ASIN (SQRT (
        lat1-abs (LAT2) * PI ()/180/2),
        2) + COS (LAT1 * PI ()/180) * COS (ABS (LAT2) *
        pi ()/180) * Power (SIN (lng1-lng2) *
        pi ()/180/2), 2));
End

--returns the "distance in kilometers", assuming a earth radius of 6,371 km.
Summary

The above mathematical theory, I do not delve into, but also refer to the relevant information. Always feel this way to achieve a good rub.

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.