c# - 从起点、终点和距离计算坐标

标签 c# coordinates

我在 map 上有两个点,我知道它们之间的距离。现在,我需要在距离起点 X 米的地方找到一个新点。但是,我不知道如何找到新坐标。

var nextTrazadoPoint = new Coord {Lat = ....,  Lng=...., Alt=...};
var previousTrazadoPoint = new Coord {Lat = ....,  Lng=...., Alt...};
var fromCoords = new GeoCoordinate(nextTrazadoPoint.Lat, nextTrazadoPoint.Lng, nextTrazadoPoint.Alt);
var toCoords = new GeoCoordinate(previousTrazadoPoint .Lat, previousTrazadoPoint .Lng, previousTrazadoPoint .Alt);
var distance = fromCoords.GetDistanceTo(toCoords); //Let's say 1000 ¿meters?

现在我想从 previousTrazadoPoint 步行 200 米到 nextTrazadoPoint

//Vector from previousTrazadoPoint to nextTrazadoPoint
var vectorDireccion = new Vector(
    (double)(nextTrazadoPoint.Latitud - previousTrazadoPoint.Latitud), 
    (double)(nextTrazadoPoint.Longitud - previousTrazadoPoint.Longitud)
    );

//Normalize
vectorDireccion.Normalize();

//meters from previousTrazadoPoint 
var distanciaARecorrer = 200;

//New coords
var vectorDestino = distanciaARecorrer * vectorDireccion;
point.Latitud = (decimal)vectorDestino.X + previousTrazadoPoint.Latitud;
point.Longitud = (decimal)vectorDestino.Y + previousTrazadoPoint.Longitud;

但是,当我在 Gmaps 上绘制新点时,它并没有放在两者之间。 有什么想法吗?

最佳答案

感谢@HansKilian 和@cletus ( Calculate distance between 2 GPS coordinates ),我找到了解决方案

private const double EARTH_RADIUS = 6378.1;
private static double ConvertToRadians(double angle)
{
    return (Math.PI / 180) * angle;
}
private static double ConvertToDegree(double angle)
{
    return angle * (180.0 / Math.PI);
}

private static double CalculateBearing(CoordsDto from, CoordsDto to)
{
    var from_lat_rad = ConvertToRadians(from.Latitud);
    var to_lat_rad = ConvertToRadians(to.Latitud);

    var dif_lng_rad = ConvertToRadians(to.Longitud - from.Longitud);

    double x = Math.Cos(from_lat_rad) * Math.Sin(to_lat_rad) - Math.Sin(from_lat_rad) * Math.Cos(to_lat_rad) * Math.Cos(dif_lng_rad);
    double y = Math.Sin(dif_lng_rad) * Math.Cos(to_lat_rad);

    // Math.Atan2 can return negative value, 0 <= output value < 2*PI expected 
    return (Math.Atan2(y, x) + Math.PI * 2) % (Math.PI * 2);
}

public static CoordsDto GetPointFarAway(CoordsDto from, CoordsDto to, double meterAwayFromStart)
{
    var resp = new CoordsDto();
    var bearing_rad = CalculateBearing(from, to);
    var d = meterAwayFromStart * 0.001; //KM

    var input_lat1_rad = ConvertToRadians(from.Latitud);
    var input_lon1_rad = ConvertToRadians(from.Longitud);

    var newPoint_lat_rad = Math.Asin(
        Math.Sin(input_lat1_rad) * Math.Cos(d / EARTH_RADIUS) + Math.Cos(input_lat1_rad) * Math.Sin(d / EARTH_RADIUS) * Math.Cos(bearing_rad)
    );
    var newPoint_lon_rad = input_lon1_rad + Math.Atan2(
        Math.Sin(bearing_rad) * Math.Sin(d / EARTH_RADIUS) * Math.Cos(input_lat1_rad),
        Math.Cos(d / EARTH_RADIUS) - Math.Sin(input_lat1_rad) * Math.Sin(newPoint_lat_rad)
    );

    resp.Latitud = ConvertToDegree(newPoint_lat_rad);
    resp.Longitud = ConvertToDegree(newPoint_lon_rad);

    return resp;
}

关于c# - 从起点、终点和距离计算坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57088584/

相关文章:

c# - 列表上的哈希函数独立于其中的项目顺序

ios - 第一次 map 头无处可去

java - 平板电脑中的 Libgdx 坐标系

javascript - 查找 LatLng Coord 是否位于其他两个 LatLng Coords 之间

c# - 如何控制 MVVM WinRT/XAML Windows Store 应用程序中 ListView 的滚动位置?

c# - C# 是否有某种 value_or_execute 或 value_or_throw?

python - 在 python GUI 中使用坐标列表创建一条线

java - 将坐标纬度和经度更改为 andress

c# - 如果处理了特定异常,如何防止中断

c# - 将 C# 客户端连接到 C++ 服务器