C# 计算球面距离(经度纬度 两点之间距离)
以下解决方案来自于网络,仅供参考
方案1
/// <summary>
/// Calculates the distance between to lat/long points and returns the approximate distance in kilometers
/// </summary>
/// <param name="from">Point in long/lat decimal degrees</param>
/// <param name="to">Point in long/lat decimal degrees</param>
/// <returns>Distance in kilometers</returns>
private double CalcDistance(Point from, Point to)
{
double rad = 6371; //Earth radius in Km
//Convert to radians
double p1X = from.X / 180 * Math.PI;
double p1Y = from.Y / 180 * Math.PI;
double p2X = to.X / 180 * Math.PI;
double p2Y = to.Y / 180 * Math.PI;
return Math.Acos(Math.Sin(p1Y) * Math.Sin(p2Y) +
Math.Cos(p1Y) * Math.Cos(p2Y) * Math.Cos(p2X - p1X)) * rad;
}
方案2
距离公式如同毕氏定理求直角三角形之斜边
a^2 + b^2 = c^2 (毕氏定理)
c = (a^2 + b^2)^0.5 (斜边长度)
而 a 为两点的x座标相减之绝对值(亦可称为距离)
b 为两点的y座标相减之绝对值
所以若两点的座标分别为 (x1, y1) , (x2, y2)
带入求斜边长度的公式可以得到
c = ((x1-x2)^2 + (y1-y2)^2)^0.5
即为两点距离
C#程式码如下:
double x1, x2, y1, y2;
Console.Write("请输入第一个点的x座标 x1=");
x1 = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入第一个点的y座标 y1=");
y1 = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入第二个点的x座标 x2=");
x2 = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入第二个点的y座标 y2=");
y2 = Convert.ToDouble(Console.ReadLine());
double distance = Math.Pow(Math.Pow((x1 - x2), 2) + Math.Pow((y1 - y2), 2), 0.5);
Console.WriteLine("两点的距离=" + distance);
方案3
c# 根据经纬度计算两点之间的距离。项目中实际使用。大家放心使用。
public static double Fun_Rad(double d)
{
return d * Math.PI / 180.0;
}
public static double Fun_GetDistance(double lat1, double lng1, double lat2, double lng2)
{
double d_EarthRadius = 6378.137;
double radLat1 = Fun_Rad(lat1);
double radLat2 = Fun_Rad(lat2);
double radLat = Fun_Rad(lat1) - Fun_Rad(lat2);
double radLng = Fun_Rad(lng1) - Fun_Rad(lng2);
double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(radLat / 2), 2) +
Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(radLng / 2), 2)));
s = s * d_EarthRadius;
s = Math.Round(s * 10000) / 10000;
return s;
}
// 计算已知经纬度的两点间的距离
// Thu 2005.08.04
using System;
struct JWD
{
double JD, JF, JM;
double WD, WF, WM;
public double Jd, Wd, J, W;
public const double Ea = 6378137; // 赤道半径
public const double Eb = 6356725; // 极半径
public readonly double Ec;
public readonly double Ed;
// 构造函数, 经度: a 度, b 分, c 秒; 纬度: d 度, e 分, f 秒
public JWD(double a, double b, double c, double d, double e, double f)
{
JD=a; JF=b; JM=c; WD=d; WF=e; WM=f;
Jd = JD + JF / 60 + JM / 3600;
Wd = WD + WF / 60 + WM / 3600;
J = Jd * Math.PI / 180;
W = Wd * Math.PI / 180;
Ec = Eb + (Ea - Eb) * (90-Wd) / 90;
Ed = Ec * Math.Cos(W);
}
}
class Test
{
static void Main()
{
JWD A0 = new JWD(120, 36, 0, 28, 0, 0);
JWD A1 = new JWD(130, 0, 0, 30, 0, 0);
Calc(A0, A1);
}
static void Calc(JWD A, JWD B)
{
double dx = (B.J - A.J) * A.Ed;
double dy = (B.W - A.W) * A.Ec;
double d = Math.Sqrt(dx * dx + dy * dy);
Console.WriteLine("{0:N1}M", d);
}
}
/* 程序输出:
949,373.0M
*/
更改:
using System;
using System.Collections.Generic;
using System.Text;
namespace localTest
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("功能: 根据两点坐标,计算两点之间的距离:");
Console.WriteLine("\n请输入坐标一的经度和纬度,中间用‘ ,’或用 空格 隔开:");
string[] onewordstr = word(Console.ReadLine());
Double onelctLong = Convert.ToDouble(onewordstr[0]);
Double onelctlat = Convert.ToDouble(onewordstr[1]);
Console.WriteLine("请输入坐标二的经度和纬度,中间用‘ ,’或用 空格 隔开:");
string[] twowordstr = word(Console.ReadLine());
Double twolctLong = Convert.ToDouble(twowordstr[0]);
Double twolctlat = Convert.ToDouble(twowordstr[1]);
locat onelct = new locat(onelctLong, onelctlat);
locat twolct = new locat(twolctLong, twolctlat);
//计算两坐标间的距离
Calc(onelct, twolct);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\n请按照格式输入正确的坐标!\n");
}
}
//将用户输入的经度纬度分开,存储在数组中
static string[] word(string inStr)
{
char[] wordchar = { ',', ' ', '\n' };
string[] wordstr = inStr.Split(wordchar);
return wordstr;
}
/// <summary>
/// 根据两点坐标,求出距离
/// </summary>
/// <param name="oneLct">坐标一</param>
/// <param name="twoLct">坐标二</param>
static void Calc(locat oneLct, locat twoLct)
{
double dx = (twoLct.LongRad - oneLct.LongRad) * oneLct.Ed;
double dy = (twoLct.latRad - oneLct.latRad) * oneLct.Ec;
double d = Math.Sqrt(dx * dx + dy * dy);
Console.WriteLine("两点间的距离为:{0:N1}M", d);
Console.ReadLine();
}
}
/// <summary>
/// 经纬度结构体
/// </summary>
struct locat
{
public double Long, lat, LongRad, latRad; //经度,纬度,经弧度,纬弧度
public const double Ea = 6378137; // 赤道半径
public const double Eb = 6356725; // 极半径
public readonly double Ec;
public readonly double Ed;
/// <summary>
/// 根据经纬度,相应的参数
/// </summary>
/// <param name="JD">经度</param>
/// <param name="WD">纬度</param>
public locat(double JD, double WD)
{
Long = JD;
lat = WD;
LongRad = Long * Math.PI / 180; //计算出经弧度
latRad = lat * Math.PI / 180; //计算出纬弧度
Ec = Eb + (Ea - Eb) * (90 - lat) / 90;
Ed = Ec * Math.Cos(latRad);
}
}
}