C# 有没有一种方法可以通过编程确定作为Bing地图中心的GPS坐标?

C# 有没有一种方法可以通过编程确定作为Bing地图中心的GPS坐标?,c#,uwp,gps,bing-maps,centering,C#,Uwp,Gps,Bing Maps,Centering,这与我的问题有点相似/相关,但差异足以让我提出一个单独的问题 当通过编程将GPS坐标数组添加到Bing地图时,我希望将地图的中心设置为数组中所有GPS坐标之间的中点。例如,如果我想展示密苏里州的汉尼拔、独立、哥伦比亚、杰斐逊城和圣路易斯,中心应该在哥伦比亚附近 我想这样做的一种方法可能是将所有纬度相加,除以上面标记为5的位置数,然后将所有经度相加,再除以该数。这两个平均值可以作为中心 这是计算中心坐标的合理方法,还是有更好的方法 我想在C中的代码中实现这一点 使现代化 以下是我对伪代码的想法:

这与我的问题有点相似/相关,但差异足以让我提出一个单独的问题

当通过编程将GPS坐标数组添加到Bing地图时,我希望将地图的中心设置为数组中所有GPS坐标之间的中点。例如,如果我想展示密苏里州的汉尼拔、独立、哥伦比亚、杰斐逊城和圣路易斯,中心应该在哥伦比亚附近

我想这样做的一种方法可能是将所有纬度相加,除以上面标记为5的位置数,然后将所有经度相加,再除以该数。这两个平均值可以作为中心

这是计算中心坐标的合理方法,还是有更好的方法

我想在C中的代码中实现这一点

使现代化 以下是我对伪代码的想法:

int locationCount = GetNumberOfLocationsOnMap(currentMap);
double totalLatitude = GetTotalLatitudesOfLocationsOnMap(currentMap);
double totalLongitude = GetTotalLongitudesOfLocationsOnMap(currentMap);
double avgLatitude = totalLatitude div locationCount;
double avgLongitude = totalLongitude div locationCount;
map.Center = avgLatitude, avgLongitude;
如果有更简单和/或更好的方法,我想知道

更新2 这是可行的,只有一个位置基于邓肯·劳勒的答案和链接:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    . . .
    SetInitialScene();
}

private async void SetInitialScene()
{
    // Set it to Monterey, California
    BasicGeoposition location = new BasicGeoposition();
    location.Latitude = 36.6002; 
    location.Longitude = -121.8947; 
    Geopoint geop = new Geopoint(location);
    await map.TrySetSceneAsync(MapScene.CreateFromLocation(geop));
}

看起来坐标边界框的中心对于这项任务会更好,因为在这种情况下,点可以在更高的缩放级别上适合屏幕

var latitudes = currentMap.locations.Select(location => location.latitude);
var longitudes = currentMap.locations.Select(location => location.longitude);
centerLatitude = (latitudes.Min() + latitudes.Max()) / 2.0;
centerLongitude = (longitudes.Min() + longitudes.Max()) / 2.0;
map.Center = centerLatitude, centerLongitude;

但是,请注意靠近极点或接近180经度的坐标,因为最小值和最大值不会提供有效的边界框。同样的问题也存在于计算平均值时。

似乎坐标边界框的中心对该任务更有效,因为在这种情况下,点可以在更高的缩放级别上适合屏幕

var latitudes = currentMap.locations.Select(location => location.latitude);
var longitudes = currentMap.locations.Select(location => location.longitude);
centerLatitude = (latitudes.Min() + latitudes.Max()) / 2.0;
centerLongitude = (longitudes.Min() + longitudes.Max()) / 2.0;
map.Center = centerLatitude, centerLongitude;

但是,请注意靠近极点或接近180经度的坐标,因为最小值和最大值不会提供有效的边界框。同样的问题也存在于计算平均值时。

如果您想让地图显示所有GPS点,您需要一个缩放级别来与中心对齐。缩放级别与贴图的宽度和高度直接相关。这是我的一个旧代码示例中的一个代码块:

/// <summary>
/// Calculates the best map view for a list of locations for a map
/// </summary>
/// <param name="locations">List of location objects</param>
/// <param name="mapWidth">Map width in pixels</param>
/// <param name="mapHeight">Map height in pixels</param>
/// <param name="buffer">Width in pixels to use to create a buffer around the map. This is to keep pushpins from being cut off on the edge</param>
public static MapViewSpecification BestMapView(IList<Location> locations, double mapWidth, double mapHeight, int buffer, out double centerLat, double centerLon, out double zoomLevel )
{
    double zoomLevel = 0;

    double maxLat = -85;
    double minLat = 85;
    double maxLon = -180;
    double minLon = 180;

    //calculate bounding rectangle
    for (int i = 0; i < locations.Count; i++)
    {
        if (locations[i].Latitude > maxLat)
        {
            maxLat = locations[i].Latitude;
        }

        if (locations[i].Latitude < minLat)
        {
            minLat = locations[i].Latitude;
        }

        if (locations[i].Longitude > maxLon)
        {
            maxLon = locations[i].Longitude;
        }

        if (locations[i].Longitude < minLon)
        {
            minLon = locations[i].Longitude;
        }
    }

    centerLat = (maxLat + minLat) / 2;
    centerLon = (maxLon + minLon) / 2;

    double zoom1=0, zoom2=0;

    //Determine the best zoom level based on the map scale and bounding coordinate information
    if (maxLon != minLon && maxLat != minLat)
    {
        //best zoom level based on map width
        zoom1 = Math.Log(360.0 / 256.0 * (mapWidth – 2*buffer) / (maxLon – minLon)) / Math.Log(2);
        //best zoom level based on map height
        zoom2 = Math.Log(180.0 / 256.0 * (mapHeight – 2*buffer) / (maxLat – minLat)) / Math.Log(2);
    }

    //use the most zoomed out of the two zoom levels
    zoomLevel = (zoom1 < zoom2) ? zoom1 : zoom2;
}

如果你想让你的地图显示你所有的GPS点,你将需要一个缩放级别去中心。缩放级别与贴图的宽度和高度直接相关。这是我的一个旧代码示例中的一个代码块:

/// <summary>
/// Calculates the best map view for a list of locations for a map
/// </summary>
/// <param name="locations">List of location objects</param>
/// <param name="mapWidth">Map width in pixels</param>
/// <param name="mapHeight">Map height in pixels</param>
/// <param name="buffer">Width in pixels to use to create a buffer around the map. This is to keep pushpins from being cut off on the edge</param>
public static MapViewSpecification BestMapView(IList<Location> locations, double mapWidth, double mapHeight, int buffer, out double centerLat, double centerLon, out double zoomLevel )
{
    double zoomLevel = 0;

    double maxLat = -85;
    double minLat = 85;
    double maxLon = -180;
    double minLon = 180;

    //calculate bounding rectangle
    for (int i = 0; i < locations.Count; i++)
    {
        if (locations[i].Latitude > maxLat)
        {
            maxLat = locations[i].Latitude;
        }

        if (locations[i].Latitude < minLat)
        {
            minLat = locations[i].Latitude;
        }

        if (locations[i].Longitude > maxLon)
        {
            maxLon = locations[i].Longitude;
        }

        if (locations[i].Longitude < minLon)
        {
            minLon = locations[i].Longitude;
        }
    }

    centerLat = (maxLat + minLat) / 2;
    centerLon = (maxLon + minLon) / 2;

    double zoom1=0, zoom2=0;

    //Determine the best zoom level based on the map scale and bounding coordinate information
    if (maxLon != minLon && maxLat != minLat)
    {
        //best zoom level based on map width
        zoom1 = Math.Log(360.0 / 256.0 * (mapWidth – 2*buffer) / (maxLon – minLon)) / Math.Log(2);
        //best zoom level based on map height
        zoom2 = Math.Log(180.0 / 256.0 * (mapHeight – 2*buffer) / (maxLat – minLat)) / Math.Log(2);
    }

    //use the most zoomed out of the two zoom levels
    zoomLevel = (zoom1 < zoom2) ? zoom1 : zoom2;
}

如果您使用的是UWP映射控件,那么有一个内置的方法可以为您执行此操作。 就打电话 MapControl.TrySetSceneAsyncMapScene.CreateFromLocationsyourListOfPointsHere;
它将计算右视图并将地图移到那里。如果您需要了解中心,可以在SetScene调用完成后查询中心属性。

如果您使用的是UWP map控件,则有一个内置方法可以为您执行此操作。 就打电话 MapControl.TrySetSceneAsyncMapScene.CreateFromLocationsyourListOfPointsHere;
它将计算右视图并将地图移到那里。如果您需要知道中心,可以在SetScene调用完成后查询中心属性。

是,根据您的用例,抓取边界框的中心将比问题中的方法更好。问题中的方法将为您提供位置的加权平均值,因此,如果您在城市中有很多点,并且有几个边远点,它将以城市为中心。另一件需要注意的事情是,根据您的显示投影,横向/纵向边界框的中心将不是显示区域的中心。例如,常用的web墨卡托投影在纬度上是非线性的,因此显示的中心不是顶部和底部纬度之间的中点。@DuncanLawler好极了!在这种情况下,最好将纬度和经度转换为墨卡托投影,然后才计算边界框,这取决于中心的含义。正如其他人提到的,Lat和Lon是球形参考,但贴图通常不是。对前者的测试是,两个点80度北纬度,0度和180度北纬度应该是北极90度纬度。但这可能不是你想要的。这对我来说不合适。我的地图的名字是简单的地图,没有位置的成员。可能是因为我的应用程序是UWP?是的,根据您的用例,抓住边界框的中心将比问题中的方法更好。问题中的方法将为您提供位置的加权平均值,因此,如果您在城市中有很多点,并且有几个边远点,它将以城市为中心。另一件需要注意的事情是,根据您的显示投影,横向/纵向边界框的中心将不是显示区域的中心。例如,常用的web墨卡托投影在纬度上是非线性的,因此显示的中心不是顶部和底部纬度之间的中点。@DuncanLawler好极了!在这种情况下,最好将纬度和经度转换为墨卡托投影,然后才计算边界boxI
这取决于你所说的中心。正如其他人提到的,Lat和Lon是球形参考,但贴图通常不是。对前者的测试是,两个点80度北纬度,0度和180度北纬度应该是北极90度纬度。但这可能不是你想要的。这对我来说不合适。我的地图的名字是简单的地图,没有位置的成员。可能是因为我的应用程序是UWP?一旦centerLat和centerLon到达,map.Center是如何分配的?假设map是Bing地图的名称。像这样:定位中心;中心纬度=中心纬度;中心经度=中心经度;map.SetViewcenter,zoomLevel?一旦centerLat和centerLon到达,地图中心是如何分配的?假设map是Bing地图的名称。像这样:定位中心;中心纬度=中心纬度;中心经度=中心经度;map.SetViewcenter,zoomLevel?