以一个经纬度为中心计算出四个顶点
public const double EARTH_RADIUS = 6378137.0;//地球半径(m)
/// <summary>
/// 以一个经纬度为中心计算出四个顶点
/// </summary>
/// <param name="distance">半径(米)</param>
/// <returns></returns>
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
};
}
/// <summary>
/// 角度数转换为弧度公式
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
private static double radians(double d)
{
return d * Math.PI / 180.0;
}
/// <summary>
/// 弧度转换为角度数公式
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
private static double degrees(double d)
{
return d * (180 / Math.PI);
}
/// <summary>
/// 代表经度, 纬度
/// </summary>
public class Poin
{
/// <param name="lat">纬度 X</param>
/// <param name="lng">经度 Y</param>
public Poin(double lat, double lng)
{
this.lat = lat;
this.lng = lng;
}
// 纬度 X
private double lat;
// 经度 Y
private double lng;
/// <summary>
/// 代表纬度 X轴
/// </summary>
public double Lat { set; get; }
/// <summary>
/// 代表经度 Y轴
/// </summary>
public double Lng { get; set; }
public double RadLat { get { return lat * Math.PI / 180; } }
public double RadLng { get { return lng * Math.PI / 180; } }
}
/// <summary>
/// 经纬度坐标
/// </summary>
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; }
}
}
/// <summary>
/// 计算坐标点的距离
/// </summary>
public void GetDistance()
{
//x经度 y纬度
string enbid = "";
double distance = 0;
double x1 = ConverToRadian(31.499605);
double x2 = ConverToRadian(31.4996);
double y1 = ConverToRadian(104.73775);
double y2 = ConverToRadian(104.73783);
double d = 0;
if (Math.Sin(x1) * Math.Sin(x2) + Math.Cos(x1) * Math.Cos(x2) * Math.Cos(y1 - y2) > 1)
d = EARTH_RADIUS * Math.Acos(1);
else if (Math.Sin(x1) * Math.Sin(x2) + Math.Cos(x1) * Math.Cos(x2) * Math.Cos(y1 - y2) < -1)
d = EARTH_RADIUS * Math.Acos(-1);
else
d = EARTH_RADIUS * Math.Acos(Math.Sin(x1) * Math.Sin(x2) + Math.Cos(x1) * Math.Cos(x2) * Math.Cos(y1 - y2));
}