///
/// 经纬度坐标
///
public class Degree
{
public Degree(double x, double y)
{
X = x;
Y = y;
}
private double x;
public double X
{
get { return x; }
set { x = value; }
}
private double y;
public double Y
{
get { return y; }
set { y = value; }
}
}
public class CoordDispose
{
private const double EARTH_RADIUS = 6378137.0;//地球半径(米)
///
/// 角度数转换为弧度公式
///
///
///
private static double radians(double d)
{
return d * Math.PI / 180.0;
}
///
/// 弧度转换为角度数公式
///
///
///
private static double degrees(double d)
{
return d * (180 / Math.PI);
}
///
/// 计算两个经纬度之间的直接距离
///
public static double GetDistance(Degree Degree1, Degree Degree2)
{
double radLat1 = radians(Degree1.X);
double radLat2 = radians(Degree2.X);
double a = radLat1 - radLat2;
double b = radians(Degree1.Y) - radians(Degree2.Y);
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;
return s;
}
///
/// 计算两个经纬度之间的直接距离(google 算法)
///
public static double GetDistanceGoogle(Degree Degree1, Degree Degree2)
{
double radLat1 = radians(Degree1.X);
double radLng1 = radians(Degree1.Y);
double radLat2 = radians(Degree2.X);
double radLng2 = radians(Degree2.Y);
double s = Math.Acos(Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Cos(radLng1 - radLng2) + Math.Sin(radLat1) * Math.Sin(radLat2));
s = s * EARTH_RADIUS;
s = Math.Round(s * 10000) / 10000;
return s;
}
///
/// 以一个经纬度为中心计算出四个顶点
///
/// 半径(米)
///
public static Degree[] GetDegreeCoordinates(Degree Degree1, double distance)
{
double dlng = 2 * Math.Asin(Math.Sin(distance / (2 * EARTH_RADIUS)) / Math.Cos(Degree1.X));
dlng = degrees(dlng);//一定转换成角度数 原PHP文章这个地方说的不清楚根本不正确 后来lz又查了很多资料终于搞定了
double dlat = distance / EARTH_RADIUS;
dlat = degrees(dlat);//一定转换成角度数
return new Degree[] { new Degree(Math.Round(Degree1.X + dlat,6), Math.Round(Degree1.Y - dlng,6)),//left-top
new Degree(Math.Round(Degree1.X - dlat,6), Math.Round(Degree1.Y - dlng,6)),//left-bottom
new Degree(Math.Round(Degree1.X + dlat,6), Math.Round(Degree1.Y + dlng,6)),//right-top
new Degree(Math.Round(Degree1.X - dlat,6), Math.Round(Degree1.Y + dlng,6)) //right-bottom
};
}
}