繁体   English   中英

从起点,终点和距离计算坐标

[英]Calculate Coord from start point, endpoint, and distance

我在地图上有两个点,我知道它们之间的距离。 现在,我需要在它们之间的新点距起点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 nextTrazadoPoint步行200米到下previousTrazadoPoint 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上绘制新点时,它不会放在两者之间。 有任何想法吗?

我看不到您的代码有什么问题。 您还没有真正提供足够的信息来获取您的代码并对其进行编译,然后查看出了什么问题,因此我尝试使用Vector2类从头开始对其进行编码。 我不能使用Vector因为它在.NET core中不可用,而我的沙盒项目是.NET core项目。

这就是我得到的

var origin = new Vector2(100.0f, 100.0f);
var destination = new Vector2(0.0f, 400.0f);
var direction = destination - origin;
var movement = Vector2.Normalize(direction) * 200.0f;
var movementdestination = origin + movement;
Console.WriteLine($"X: {movementdestination.X}, Y: {movementdestination.Y}");

它打印

X: 36.75444, Y: 289.7367

据我所知,这是正确的。 希望对您有所帮助。

感谢@HansKilian和@cletus( 计算2个GPS坐标之间的距离 ),我可以找到解决方案

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM