According to the latitude and longitude, calculate the nearest surrounding business [Turn]

Abstract: The current work requires the mobile phone to find the nearby merchants within N meters. The function is as follows. The database records the latitude and longitude of the merchants marked on Baidu (eg: 116.412007, 39.947545). The initial idea is to take the center point as the center point and make a loop on the radius , every time the radius increases by one pixel (tentatively set to 1 meter), then loop the perimeter, and check the business of the corresponding point in the database (it's a long-term circular job). There are similar articles on the Internet on Baidu. The general idea is known. A center point, a radius, find all the points contained in the circle parabola. In this case, you need to know the vertices of the diagonal of the circle required. The problem is that the latitude and longitude is a point, and the radius is a distance Recorded in the



database The latitude and longitude marked by the merchant on Baidu (such as: 116.412007, 39.947545), the

initial idea is to use the center point as the center point, loop the radius, and loop the perimeter every time the radius increases by one pixel (tentatively 1 meter), and store it in the database Querying the merchants at the corresponding point (it's really a long-term cycle work),

and similar articles on Baidu have some ideas on the Internet. The

general idea is to know a center point and a radius, and find all the points contained in the circle in the circular parabola. In this case, it is necessary to Knowing the vertices of the diagonal of the required circle, the problem is that the latitude and longitude is a point, and the radius is a distance, which cannot be directly added or subtracted.

Finally found the article I wanted

http://digdeeply.org/archives/06152067.htmlReference

The original article lz was changed to a C# class.

Reference attachment 11111

The following is the code:
/// <summary>
     /// latitude and longitude coordinates
     /// </summary>    
   public class Degree
     {
         public Degree(double x, double y)
         {
             X = x;
             and = and;
         }
         private double x;
 
         public double X
         {
             get { return x; }
             set { x = value; }
         }
         private double y;
 
         public double Y
         {
             get { return y; }
             set { y = value; }
         }
     }
 
      
     public class CoordDispose
     {
         private const double EARTH_RADIUS = 6378137.0;//Earth radius (meters)
 
         /// <summary>
         /// Convert angle to radian formula
         /// </summary>
         /// <param name="d"></param>
         /// <returns></returns>
         private static double radians(double d)
         {
             return d * Math.PI / 180.0;
         }
 
         /// <summary>
         /// Convert radians to degrees formula
         /// </summary>
         /// <param name="d"></param>
         /// <returns></returns>
         private static double degrees(double d)
         {
             return d * (180 / Math.PI);
         }
 
         /// <summary>
         /// Calculate the direct distance between two latitude and longitude
         /// </summary>
 
         public static double GetDistance(Degree Degree1, Degree Degree2)
         {
             double radLat1 = radians(Degree1.X);
             double radLat2 = radians(Degree2.X);
             double a = radLat1 - radLat2;
             double b = radians(Degree1.Y) - radians(Degree2.Y);
 
             double s = 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;
             return s;
         }
 
         /// <summary>
         /// Calculate the direct distance between two latitude and longitude (google algorithm)
         /// </summary>
         public static double GetDistanceGoogle(Degree Degree1, Degree Degree2)
         {
             double radLat1 = radians(Degree1.X);
             double radLng1 = radians(Degree1.Y);
             double radLat2 = radians(Degree2.X);
             double radLng2 = radians(Degree2.Y);
 
             double s = Math.Acos(Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Cos(radLng1 - radLng2) + Math.Sin(radLat1) * Math.Sin(radLat2));
             s = s * EARTH_RADIUS;
             s = Math.Round(s * 10000) / 10000;
             return s;
         }
 
         /// <summary>
         /// Calculate four vertices with a latitude and longitude as the center
         /// </summary>
         /// <param name="distance">半径(米)</param>
         /// <returns></returns>
         public static Degree[] GetDegreeCoordinates(Degree Degree1, double distance)
         {
             double dlng = 2 * Math.Asin(Math.Sin(distance / (2 * EARTH_RADIUS)) / Math.Cos(Degree1.X));
             dlng = degrees(dlng);//It must be converted into degrees. The original PHP article is not clear and it is not correct at all. Later, lz checked a lot of information and finally got it
 
             double dlat = distance / EARTH_RADIUS;
             dlat = degrees(dlat);//It must be converted into degrees
 
             return new Degree[] { new Degree(Math.Round(Degree1.X + dlat,6), Math.Round(Degree1.Y - dlng,6)),//left-top
                                   new Degree(Math.Round(Degree1.X - dlat,6), Math.Round(Degree1.Y - dlng,6)),//left-bottom
                                   new Degree(Math.Round(Degree1.X + dlat,6), Math.Round(Degree1.Y + dlng,6)),//right-top
                                   new Degree(Math.Round(Degree1.X - dlat,6), Math.Round(Degree1.Y + dlng,6)) //right-bottom
             };
 
         }
     }

        // call the method
        static void Main(string[] args)
        {
            double a = CoordDispose.GetDistance(new Degree(116.412007, 39.947545), new Degree(116.412924, 39.947918));//116.416984,39.944959
            double b = CoordDispose.GetDistanceGoogle(new Degree(116.412007, 39.947545), new Degree(116.412924, 39.947918));
            Degree[] dd = CoordDispose.GetDegreeCoordinates(new Degree(116.412007, 39.947545), 102);
            Console.WriteLine(a+" "+b);
            Console.WriteLine(dd[0].X + "," + dd[0].Y );
            Console.WriteLine(dd[3].X + "," + dd[3].Y);
            Console.ReadLine();
        }



The method calculated in the database
6371.004 Earth radius
6371.004*ACOS(SIN(@GPSLat/180*PI())*SIN(@Lat/180*PI())+COS(@GPSLat/180*PI())*COS( @Lat/180*PI())*COS((@GPSLng-@Lng)/180*PI())) The calculation formula can be found in the detailed explanation of the spherical arc length calculation formula [The arc length is the distance between two points 】

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326586559&siteId=291194637