Coordinate distance algorithm

Source: Internet
Author: User
Tags asin cos pow sin

After two days of research, I think there are some things that need to be written and also help people

  This is an algorithm to find nearby people, looking for this article on the Internet http://blog.csdn.net/dyllove98/article/details/9795815, his calculation of the smallest square four vertices a bit of a problem.


The first step, we think, if we want to find the nearby five kilometers of people, is not the first thought according to their latitude and longitude (if Lian degrees do not know how, I think you should know degrees Niang), and then to the database to get other people's latitude and longitude, according to two latitude and longitude to obtain their distance,

If the distance is less than 5 km, then the condition is met.

Get distance in two cases, the first is to find out in the code to calculate, the second case is to write a function in SQL, in the database is counted

code calculation distance algorithm:

 

     Public Static Doublegetdistance (degree Degree1, degree Degree2) {DoubleRADLAT1 =radians (DEGREE1.Y); DoubleRADLAT2 =radians (DEGREE2.Y); DoubleA = RADLAT1-RadLat2; Doubleb = radians (degree1.x)-radians (degree2.x); Doubles =2* Math.asin (Math.sqrt (Math.pow (Math.sin (A/2),2) +Math.Cos (RADLAT1)* MATH.COS (RADLAT2) * MATH.POW (Math.sin (b/2),2))); S= S *Earth_radius; S= Math.Round (S *10000) /10000; returns; }

radians is an algorithm of angle to Radian

private static double degrees (double D)         {             return d * (180/MATH.PI);         }

If you do not know the inside of the algorithm is a kind of principle, you can Baidu related trigonometric function formula

SQL Distance algorithm

  

SETAnsi_nulls onGOSETQuoted_identifier onGOALTER function [dbo].[fngetdistance](@LatBegin float,@LngBegin float,@LatEnd float,@LngEnd float)returns float   asbegin  --distance (km)  Declare @Distance float; Declare @EARTH_RADIUS float; Set @EARTH_RADIUS = 6378.137; Declare @RadLatBegin float,           @RadLatEnd float,          @RadLatDiff float,          @RadLngDiff float; Set @RadLatBegin = @LatBegin * Pi()/180.0; Set @RadLatEnd = @LatEnd * Pi()/ the.; Set @RadLatDiff = @RadLatBegin - @RadLatEnd; Set @RadLngDiff = @LngBegin * Pi()/180.0 - @LngEnd * Pi()/180.0; Set @Distance = 2 * ASIN(sqrt(Power(Sin(@RadLatDiff/2),2)+ Cos(@RadLatBegin)                  * Cos(@RadLatEnd)* Power(Sin(@RadLngDiff/2),2))); Set @Distance = @Distance * @EARTH_RADIUS; --Set @Distance = Round (@Distance * 10000)/10000  return @Distance End

 

If you agree with me above, I can only say to you that processing small amounts of data is also possible.

For example: We now want to do a parking management system, the need for parking spaces can be found in the vicinity of the parking space, for parking spaces can be found near the need for parking spaces, if the database has 10,000 of the latitude and longitude of the parking space, the need for parking access server, server to do 100,000 distance conversion, if there are 10 people to visit, The server is accessed by your easy-to-reach million-data volume.

So what can be solved?

  

First look at this picture, we know the origin of the coordinates of our latitude and longitude, also know the radius (a few kilometers nearby), you can get the smallest square of the circle, so according to the formula can get the square four vertices to know the minimum latitude and longitude and maximum latitude and longitude

The latitude and longitude we get to the database inside a check, it can be a one-off to meet the scope of the people to check

Come out.

The distance algorithm of four vertices is obtained according to latitude and longitude and distance.

 Private Static voidGetlatlon (DoubleGlon,DoubleGlat,DoubleDistanceDoubleAngle out DoubleNewLon, out DoubleNewlat) {             DoubleEa =6378137;//Equatorial Radius             DoubleEb =6356725;//Polar radius             DoubleDX = distance * Math.sin (angle * Math.PI/180.0); DoubleDY = distance * Math.Cos (angle * Math.PI/180.0); //Double EC = 6356725 + 21412 * (90.0-glat)/90.0; //21412 is the difference between the equatorial radius and the polar radius.             DoubleEC = Eb + (EA-EB) * (90.0-Glat)/90.0; Doubleed = EC * Math.Cos (Glat * Math.PI/ the); NewLon= (dx/ed + Glon * Math.PI/180.0) *180.0/Math.PI; Newlat= (Dy/ec + Glat * Math.PI/180.0) *180.0/Math.PI; }          Public StaticDegree[] Getrectrange (DoubleCentorlogitude,DoubleCentorlatitude,Doubledistance) {             Doubletemp =0.0; DoubleMaxlatitude; DoubleMinlatitude; DoubleMaxlongitude; DoubleMinlongitude; Getlatlon (Centorlogitude, centorlatitude, distance,0, outTemp outmaxlatitude); Getlatlon (Centorlogitude, centorlatitude, distance, the, outTemp outminlatitude); Getlatlon (Centorlogitude, centorlatitude, distance, -, outMaxlongitude, outtemp); Getlatlon (Centorlogitude, centorlatitude, distance, the, outMinlongitude, outtemp); Maxlatitude= Math.Round (Maxlatitude,6); Minlatitude= Math.Round (Minlatitude,6); Maxlongitude= Math.Round (Maxlongitude,6); Minlongitude= Math.Round (Minlongitude,6); return Newdegree[] {NewDegree (minlongitude,maxlatitude),//Left-top                NewDegree (minlongitude,minlatitude),//Left-bottom                NewDegree (maxlongitude,maxlatitude),//Right-top                NewDegree (maxlongitude,minlatitude)//Right-bottom             }; }

These things have been tested less than 1, 2 meters

Or the words if you do not understand the place please contact me qq:209229923, or in the post for my message

Coordinate distance algorithm

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.