Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有计算坐标间英里数的算法吗?_C#_Geolocation_Bing Maps - Fatal编程技术网

C# 有计算坐标间英里数的算法吗?

C# 有计算坐标间英里数的算法吗?,c#,geolocation,bing-maps,C#,Geolocation,Bing Maps,我希望能够在Windows 8/Store应用程序中显示Bing地图,并在缩放设置下显示一组图钉/航路点,以显示每个位置,但仅此而已-瞧,我希望在显示所有位置/坐标的同时尽可能多地显示细节 我有一个伪代码: public static int GetMapZoomSettingForCoordinates(List<String> coordinatesList) { string furthestNorth = GetFurthestNorth(coordinatesLis

我希望能够在Windows 8/Store应用程序中显示Bing地图,并在缩放设置下显示一组图钉/航路点,以显示每个位置,但仅此而已-瞧,我希望在显示所有位置/坐标的同时尽可能多地显示细节

我有一个伪代码:

public static int GetMapZoomSettingForCoordinates(List<String> coordinatesList)
{
    string furthestNorth = GetFurthestNorth(coordinatesList);
    string furthestSouth = GetFurthestSouth(coordinatesList);
    string furthestEast = GetFurthestEast(coordinatesList);
    string furthestWest = GetFurthestWest(coordinatesList);
    int milesBetweenNorthAndSouthExtremes = GetMilesBetween(furthestNorth, furthestSouth);
    int milesBetweenEastAndWestExtremes = GetMilesBetween(furthestEast, furthestWest);
    int greaterCardinalDistance = Math.Max(milesBetweenNorthAndSouthExtremes, milesBetweenEastAndWestExtremes);
    return GetZoomSettingForDistance(greaterCardinalDistance);
}
…但最关键的是功能之间的距离。是否存在计算两个坐标之间里程的现有算法

我确实意识到,这是一组以美国为中心的代码,用于计算英里数和公里数;就目前而言,这是按计划进行的

使现代化 这是我的新伪代码实际编译代码,但未经测试:

public static int GetMapZoomSettingForCoordinates(List<string> coordinatePairsList)
{
    List<double> LatsList = new List<double>();
    List<double> LongsList = new List<double>();
    List<string> tempList = new List<string>();

    foreach (string s in coordinatePairsList)
    {
        tempList.AddRange(s.Split(';'));
        double dLat;
        double.TryParse(tempList[0], out dLat);
        double dLong;
        double.TryParse(tempList[0], out dLong);
        LatsList.Add(dLat);
        LongsList.Add(dLong);
        tempList.Clear();
    }

    double furthestNorth = GetFurthestNorth(LatsList);
    double furthestSouth = GetFurthestSouth(LatsList);
    double furthestEast = GetFurthestEast(LongsList);
    double furthestWest = GetFurthestWest(LongsList);
    int milesToDisplay = 
        HaversineInMiles(furthestWest, furthestNorth, furthestEast, furthestSouth);
    return GetZoomSettingForDistance(milesToDisplay);
}

private static double GetFurthestNorth(List<double> longitudesList)
{
    double northernmostVal = 0.0;
    foreach (double d in longitudesList)
    {
        if (d > northernmostVal)
        {
            northernmostVal = d;
        }
    }
    return northernmostVal;
}
…我仍然不知道GetZoomSettingForDistance应该做什么,但是

更新2 这样更好:

public static int GetMapZoomSettingForCoordinates(List<Tuple<double, double>> coordinatePairsList)
{
    var LatsList = new List<double>();
    var LongsList = new List<double>();

    foreach (Tuple<double,double> tupDub in coordinatePairsList)
    {
        LatsList.Add(tupDub.Item1);
        LongsList.Add(tupDub.Item2);
    }

    double furthestNorth = GetFurthestNorth(LongsList);
    double furthestSouth = GetFurthestSouth(LongsList);
    double furthestEast = GetFurthestEast(LatsList);
    double furthestWest = GetFurthestWest(LatsList);
    int milesToDisplay =
        HaversineInMiles(furthestWest, furthestNorth, furthestEast, furthestSouth); 
    return GetZoomSettingForDistance(milesToDisplay);
}
更新3
我意识到我的逻辑是倒退的,或者至少是错误的,关于经度的子午线和纬度的平行线。虽然经度子午线是由北向南绘制的垂直线,反之亦然,纬度平行线是由东向西绘制的水平线,但沿着这些线的点表示基于纬度平行线的南北位置,并表示基于经度子午线的东西位置。这在我的脑海中似乎是倒退的,直到我想象出这些线在地球上的经度和纬度上旋转,而不是像土星环那样简单地围绕地球旋转;帮助我正确理解的另一个原因是提醒自己,经度子午线的值决定了一个人所处的时区。因此,上面的代码应该更改为通过纬度来确定最北端和最南端,反之,通过经度来确定最东端和最西端。

要获得直线距离,请使用毕达哥拉斯定理来找到斜边

d=增量x^2+增量y^2^.5

基本上,将x方向和y方向的变化平方,相加,然后取平方根

在您的伪代码中,看起来您可能有许多点,并且您希望找到一个包含所有点的最大距离,如果您试图计算地图缩放的比例,这是有意义的。同样的公式也适用,只需使用delta x的南北极端之间的miles,以及delta y的南北极端之间的miles。您可以选择添加一个固定的数量,以确保地图边缘没有点。

您可以使用公式计算沿球体表面的距离

使用地球作为球体的大小来计算距离。它很容易转换成C


请注意,如果您只想从横向或纵向找到您想要做的距离,则可以简化公式。

为了确保,您正在寻找两点之间的直线距离,不是从位置a到位置B使用道路的路径长度?这些坐标是纬度和经度的形式吗?如果是这样的话:艾玛特尔:是的,直线。杰夫:是的。谢谢。我想你不是说直线。你是说弧线穿过地球表面我真正的意思是,乌鸦在飞。我并不关心确切的距离,我只想显示地图上的所有位置,同时显示尽可能多的细节-缩小到每个方向上最远的点尽可能靠近地图边缘的点。这在计算沿球体表面的弧长时是不准确的。