1.首先定义一个点与点之间测算距离的方法
2.然后定义找出基本点和集合中最近的一个点的方法
3.取第一条数据即是最近的点的坐标
1 public class Location 2 { 3 public int id { set; get; }//地点的id 4 public double lat { set; get; } 5 public double lng { set; get; } 6 public double distance { set; get; }//地点的距离 7 }
1 private const double EARTH_RADIUS = 6378.137;//地球半径 2 private double rad(double d) 3 { 4 return d * Math.PI / 180.0; 5 } 6 //根据两个点的左边测算出距离 7 public double GetDistance(Location location1, Location location2) 8 { 9 double radLat1 = rad(location1.lat); 10 double radLat2 = rad(location2.lat); 11 double a = radLat1 - radLat2; 12 double b = rad(location1.lng) - rad(location2.lng); 13 14 double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + 15 Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))); 16 s = s * EARTH_RADIUS; 17 s = Math.Round(s * 10000) / 10000; 18 return s; 19 } 20 //找出基本点和数组中最近的一个点 21 public List<Location> GetDistance(Location location1, List<Location> location2) 22 { 23 Location lo=new Location(); 24 foreach (var item in location2) 25 { 26 lo.lat=item.lat; 27 lo.lng=item.lng; 28 item.distance = GetDistance(location1, lo); 29 } 30 return location2.OrderBy(c=>c.distance).ToList(); 31 }