您的位置:首页 > 其它

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

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

主要是思路。请大家指正

public static void CheckGps()
{

double temp = Common.Jl; //Common.Jl 指定的查找距离。单位(米)
Common.Test dd = new Common.Test(); //查询的对象
bool find = false;
//MessageBox.Show("Test");

foreach (Common.Test test in Common.testList)
{
double jl = GetDistance(test.lat, test.lon, Common.gpsinfo.latitude, Common.gpsinfo.longitude);
if (Common.Jl > jl) //如果在查询范围内
{
if (jl < temp) //如果比当前最近坐标还近
{
find = true;
temp = jl; //当前为最近距离的对象
dd = test;
}
}
}
if (find)
{
MessageBox.Show(dd.name + "距离:" + temp.ToString());
Common.xl = false;
}
}

public static double rad(double d)
{
return d * Math.PI / 180.0;
}
public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
double EARTH_RADIUS = 6378.137;
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
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;
s = s * 1000;
return s;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: