首先获取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地址的纬度值
}
}
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;
}
{
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;
}