Calculate distance based on latitude and longitude

I found it online, it's easy to use MARK

Functional requirements : It is necessary to query other sites within 5 kilometers of a site (the latitude and longitude of the site is known).

Method 1  : traverse each record, and calculate the distance with each point in the database. When the distance is less than 5 kilometers, it is considered a match (extremely inefficient and time-consuming).

Method 2  : Filter out the approximate latitude and longitude range before calculating. Although what we are seeking is a circle, we can first find the circumscribed square of the circle (for example, the four points of the square), first calculate the longitude and latitude of the four points of the rectangle around the point, and then use the longitude and latitude of the square to directly Match the records in the database, and then use method 1 to filter (it is much more efficient than only using method 1  - rational use of the algorithm ).

public  static  class DistanceHelper
    {
        ///  <summary> 
        /// According to a given latitude and longitude point and distance, query nearby places
         ///  </summary> 
        ///  <param name="longitude"> longitude </param> 
        ///  < param name="latitude"> latitude </param> 
        ///  <param name="distance"> distance (unit: km or km) </param> 
        ///  <returns> returns 4 points of a range, Minimum latitude and latitude, maximum longitude and latitude </returns> 
        public  static PositionModel FindNeighPosition( double longitude, double latitude, double distance)
        {
            // First calculate the latitude and longitude range of the query point   
            double r = 6378.137 ; // Earth radius in kilometers   
            double dis = distance; // Kilometer distance     
            double dlng = 2 * Math.Asin(Math.Sin(dis / ( 2 * r) ) / Math.Cos(latitude * Math.PI / 180 ));
            dlng = dlng * 180 / Math.PI; // Convert the angle 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;
            return new PositionModel
            {
                MinLat = minlat,
                MaxLat = maxlat,
                MinLng = minlng,
                MaxLng = maxlng
            };
        }

        ///  <summary> 
        /// Calculate the distance between two points, return the distance between two points, unit: kilometers or kilometers
         /// This formula is provided by GOOGLE, the error is less than 0.2 meters
         ///  </summary> 
        // /  <param name="lat1"> First point latitude </param> 
        ///  <param name="lng1"> First point longitude </param> 
        ///  <param name="lat2"> Second point Latitude </param> 
        ///  <param name="lng2"> Longitude of the second point </param> 
        ///  <returns> Returns the distance between two points in kilometers or kilometers </returns> 
        public  static  double GetDistance ( double lat1,double lng1, double lat2, doublelng2)
        {
            //地球半径,单位米
            double EARTH_RADIUS = 6378137;
            double radLat1 = Rad(lat1);
            double radLng1 = Rad(lng1);
            double radLat2 = Rad(lat2);
            double radLng2 = Rad(lng2);
            double a = radLat1 - radLat2;
            double b = radLng1 - radLng2;
            double result = 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))) * EARTH_RADIUS;
            return result / 1000;
        }

        ///  <summary> 
        /// Convert latitude and longitude into radians
         ///  </summary> 
        ///  <param name="d"></param> 
        ///  <returns></returns> 
        private  static  double Rad( double d)
        {
            return (double)d * Math.PI / 180d;
        }
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020070&siteId=291194637