您的位置:首页 > 其它

根据GPS经纬度查找指定范围内的对象

2008-12-10 09:41 603 查看
项目上需要根据当前的经纬度查询指定范围内离这个坐标最近的对象。

因为是DEMO,代码风格,对象命名都很不规范。。 主要是思路。请大家指正


1

2 public static void CheckGps()

3 {

4

5 double temp = Common.Jl; //Common.Jl 指定的查找距离。单位(米)

6 Common.Test dd = new Common.Test(); //查询的对象

7 bool find = false;

8 //MessageBox.Show("Test");

9

10 foreach (Common.Test test in Common.testList)

11 {

12 double jl = GetDistance(test.lat, test.lon, Common.gpsinfo.latitude, Common.gpsinfo.longitude);

13 if (Common.Jl > jl) //如果在查询范围内

14 {

15 if (jl < temp) //如果比当前最近坐标还近

16 {

17 find = true;

18 temp = jl; //当前为最近距离的对象

19 dd = test;

20 }

21 }

22 }

23 if (find)

24 {

25 MessageBox.Show(dd.name + "距离:" + temp.ToString());

26 Common.xl = false;

27 }

28 }

29

30

31 public static double rad(double d)

32 {

33 return d * Math.PI / 180.0;

34 }

35 public static double GetDistance(double lat1, double lng1, double lat2, double lng2)

36 {

37 double EARTH_RADIUS = 6378.137;

38 double radLat1 = rad(lat1);

39 double radLat2 = rad(lat2);

40 double a = radLat1 - radLat2;

41 double b = rad(lng1) - rad(lng2);

42 double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +

43 Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));

44 s = s * EARTH_RADIUS;

45 s = Math.Round(s * 10000) / 10000;

46 s = s * 1000;

47 return s;

48 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: