Functional requirements : a need to query the site (the site known latitude and longitude) of other sites within a 5 km radius.
Method a : each record, to be traversed, distance calculation with each point in the database, when the distance is less than five kilometers, that match (highly inefficient, time-consuming).
Method two : first filtering the approximate range of latitude and longitude and then calculated. Although we strike a round, but we can first find an external square the circle (say, a square four points), first calculate the latitude and longitude of the four points of the rectangle around the point, then go directly to the square of latitude and longitude matching records in the database, then a method of screening (with respect to only a highly efficient method of a very large number - the rational use of the algorithm ).
public static class DistanceHelper { /// <Summary> /// a given latitude and longitude points and distance, nearby location query based /// </ Summary> /// <param name = "longitude"> longitude </ param> /// < param name = "latitude"> latitude </ param> /// <param name = "distance"> distance: (in one thousand meters or kilometers) </ param> /// <returns> returns a range of 4 points, and latitude, minimum latitude, maximum latitude and longitude </ Returns> public static PositionModel FindNeighPosition ( Double longitude, Double latitude, Double Distance) { // to calculate the latitude and longitude range query point Double R & lt = 6378.137 ; // Earth radius one thousand meters Double DIS = Distance; // km Distance Double dlng = 2 * Math.asin (Math.Sin (DIS / ( 2 * R & lt) ) / Math.cos (Latitude * Math.PI / 180 [ )); dlng = dlng * 180 [ / Math.PI; // angle into radian Double DLAT = DIS / R & lt; 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> /// calculated from two positions, the return distance of two units: one thousand meters or kilometers /// the formula GOOGLE provided, error is less than 0.2 m /// </ Summary> // / <param name = "LAT1"> first Bf </ param> /// <param name = "LNG 1"> first longitude </ param> /// <param name = "LAT2"> second point latitude </ param> /// <param name = "lng2"> second longitude </ param> /// <returns> return distance between two points, unit: km or one thousand m </ returns> public static Double of GetDistance ( Double lat1,double lng1, double lat2, double lng2) { //地球半径,单位米 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> /// Coordinates converted to radians /// </ Summary> /// <param name = "D"> </ param> /// <Returns> </ Returns> Private static Double Rad ( Double D) { return (double)d * Math.PI / 180d; } }
If a better way, please leave a message Thank you!