1. To achieve the " nearby People " function, and then to study the following:
(1) The first thing to do is to get to the latitude and longitude of their current position
(2) then upload your own data to the server
(3) The server is calculated and then the data of the person near the maximum distance to the project definition is transmitted to the foreground .
(4) Reception data to show
The most important is actually the latitude and longitude of the distance calculation:
Source: Https://github.com/feicien/studydemo
Mobile-Phone Project: Nearbydemo
Server-side project: Nearbyserverdemo
2. Mobile-Phone Code explanation:
Mainactivity is the entry activity for the project:
1 @Override2 protected voidonCreate (Bundle savedinstancestate) {3 BooleanFirst = Getsharedpreferences ("userinfo", Context.mode_private). Getboolean ("First",false);4 if(!First ) {5Intent Intent =NewIntent ( This, Loginactivity.class );6 startactivity (intent);7 }8 ....9}
(1) To view nearby people, is the need to use user information, so in the OnCreate method to determine whether the user is the first time to open the application, if it is the first time to open the application, jump to loginactivity, user information registration, then enter the mainactivity.
(2) Click on the person near the Actionbar, will display the user information obtained from the server (currently the server is all user information returned)
(3) The request network uses the volley that Google launched at IO Conference.
The server side is written using the Java Web. This is not described in detail here. The logic for calculating distances is pulled from Android's provided interface (Location.distancebetween) and should be the most accurate method:
The following is a very precise method:
1 Public Static DoubleComputedistance (DoubleLAT1,DoubleLon1,2 DoubleLAT2,DoubleLon2) {3 //Based onhttp://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf4 //using the "Inverse Formula" (Section 4)5 intMaxiters = 20;6 //Convert Lat/long to radians7LAT1 *= math.pi/180.0;8LAT2 *= math.pi/180.0;9Lon1 *= math.pi/180.0;TenLon2 *= math.pi/180.0; One DoubleA = 6378137.0;//WGS84 Major Axis A Doubleb = 6356752.3142;//WGS84 semi-major Axis - Doublef = (A-B)/A; - DoubleASQMINUSBSQOVERBSQ = (A * A-c * b)/(b *b); the DoubleL = Lon2-Lon1; - DoubleA = 0.0; - DoubleU1 = Math.atan ((1.0-f) *Math.tan (LAT1)); - DoubleU2 = Math.atan ((1.0-f) *Math.tan (LAT2)); + DoubleCosU1 =Math.Cos (U1); - DoubleCosU2 =Math.Cos (U2); + DoubleSinU1 =Math.sin (U1); A DoubleSinU2 =Math.sin (U2); at DoubleCOSU1COSU2 = cosU1 *cosU2; - DoubleSINU1SINU2 = sinU1 *sinU2; - DoubleSigma = 0.0; - DoubleDeltasigma = 0.0; - DoubleCossqalpha = 0.0; - DoubleCOS2SM = 0.0; in DoubleCossigma = 0.0; - DoubleSinsigma = 0.0; to DoubleCoslambda = 0.0; + DoubleSinlambda = 0.0; - Doublelambda = L;//Initial guess the for(intiter = 0; ITER <</span> maxiters; iter++) { * DoubleLambdaorig =Lambda; $Coslambda =Math.Cos (lambda);Panax NotoginsengSinlambda =Math.sin (lambda); - DoubleT1 = cosU2 *Sinlambda; the DoubleT2 = cosU1 * sinu2-sinu1 * cosU2 *Coslambda; + DoubleSinsqsigma = T1 * T1 + t2 * T2;//(+) ASinsigma =math.sqrt (sinsqsigma); theCossigma = sinu1sinu2 + cosu1cosu2 * COSLAMBDA;//(a) +Sigma = Math.atan2 (Sinsigma, Cossigma);//(+) - DoubleSinalpha = (Sinsigma = = 0)? 0.0 : $COSU1COSU2 * SINLAMBDA/SINSIGMA;//(+) $Cossqalpha = 1.0-sinalpha *Sinalpha; -COS2SM = (Cossqalpha = = 0)? 0.0 : -cosSigma-2.0 * SINU1SINU2/COSSQALPHA;//() the Doubleusquared = Cossqalpha * ASQMINUSBSQOVERBSQ;//defn -A = 1 + (usquared/16384.0) *//(3)Wuyi(4096.0 + usquared * the( -768 + usquared * (320.0-175.0 *( usquared) )); - DoubleB = (usquared/1024.0) *//(4) Wu(256.0 + usquared * -( -128.0 + usquared * (74.0-47.0 *( usquared) )); About DoubleC = (f/16.0) * $Cossqalpha * -(4.0 + f * (4.0-3.0 * cossqalpha));//(Ten) - DoubleCOS2SMSQ = COS2SM *COS2SM; -Deltasigma = B * Sinsigma *//(6) A(COS2SM + (b/4.0) * +(Cossigma * ( -1.0 + 2.0 * cos2smsq)- the(b/6.0) * COS2SM * -( -3.0 + 4.0 * Sinsigma * sinsigma) * $(-3.0 + 4.0 *( cos2smsq) )); theLambda = L + the(1.0-c) * f * sinalpha * the(Sigma + C * Sinsigma * the(COS2SM + C * Cossigma * -( -1.0 + 2.0 * COS2SM * cos2sm)));//(one) in DoubleDelta = (Lambda-lambdaorig)/Lambda; the if(Delta <</span> 1.0e-12) (Math.Abs)) { the Break; About } the } the returnb * A * (Sigma-deltasigma); the}
There is also a simple method that is worse than the above accuracy, as follows:
1 Public Static DoubleGetdistance (DoubleLAT1,DoubleLONGT1,DoubleLAT2,DoubleLONGT2) {2 DoublePI = 3.14159265358979323;//Pi3 DoubleR = 6371229;//the radius of the Earth4 Doublex, y, distance;5x = (LONGT2-LONGT1) * PI * R * Math.Cos (((LAT1 + lat2)/2) * pi/180)/180;6y = (LAT2-LAT1) * PI * r/180;7Distance =math.hypot (x, y);8 returndistance;9}
Android Advanced note 03:android app to see the "people nearby" feature