For the distance calculation

A UDF (user defined function) to calculate distance between two zip codes as follow:
首先获取zip地址对应的经纬度值,从zip表中可以得到。
string sqlSel = "select latitud,longitud from ziptable where zip_cd = " + zip;
using (SqlDataReader dr = SqlHelper.ExecuteReader(strCon, CommandType.Text, sqlSel))
{
    
if (dr.Read())
    {
        latitud 
= Convert.ToDouble(dr[0]);    //得到zip地址的经度值
        longitud = Convert.ToDouble(dr[1]);  //得到zip地址的纬度值
    }
}

计算任意两个zip之间的距离:
其中,参数latitud,longitud为其中一个zip的经纬度;lat,lon为另一个zip的经纬度。然后计算两个zip之间的距离。

private double GetDistance(double latitud, double longitud, double lat, double lon)
{
    distance 
=
    Math.
Round(3959 * Math.Atan(Math.Sqrt(1 - ((Math.Sin(latitud / 57.3* Math.Sin(lat / 57.3+
    Math.
Cos(latitud / 57.3* Math.Cos(lat / 57.3* Math.Cos(lon / 57.3 - longitud / 57.3)) * (Math.Sin(latitud / 57.3
    
* Math.Sin(lat / 57.3+ Math.Cos(latitud / 57.3* Math.Cos(lat / 57.3* Math.Cos(lon / 57.3 - longitud / 57.3)))) 
    
/ (Math.Sin(latitud / 57.3* Math.Sin(lat / 57.3+ Math.Cos(latitud / 57.3* Math.Cos(lat / 57.3* 
    Math.
Cos(lon / 57.3 - longitud / 57.3))), 2);
    
return distance;
}

 

posted @ 2010-06-23 13:54  Sunny Qiang  阅读(242)  评论(0)    收藏  举报