c# - 当只有一个图钉时,如何确定 Bing map 的正确缩放级别?

标签 c# .net wpf winforms bing-maps

我在 map 上添加了一些图钉,然后“调整大小”或“正确缩放”它,以便显示所有图钉,但远边缘的图钉几乎不在边界内,如下所示:
enter image description here
但是,如果只有一个图钉(如以下爱荷华州的情况,它只有一场内战),而不是尽可能接近零,它会一直缩小到 Skylab,如下所示:
enter image description here
这是我用来“调整大小” map 的代码:

private void RightsizeZoomLevelForAllPushpins()
{
    try
    {
        // Set the view so that all pushpins are displayed, with a bit of a margin around them
        // from https://stackoverflow.com/questions/65779504/how-to-set-the-zoom-level-of-bing-map-to-just-wide-enough-to-display-all-pushpin/65781319#65781319
        var map = this.userControl11.myMap;
        var locations = map.Children.OfType<Pushpin>().Select(x => x.Location);
        //Margin
        var w = new Pushpin().Width;
        var h = new Pushpin().Height;
        var margin = new Thickness(w / 2, h, w / 2, 0);

        //Set view
        map.SetView(locations, margin, 0);
        currentZoomLevel = Convert.ToInt32(map.ZoomLevel);
        UncheckAllZoomLevelToolstripMenuItems();
        SetZoomMenuItem();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}
如何使单图钉 map 不仅居中而且尽可能放大?
更新
将括号添加到对 Count 的两个引用,并在 else 块中添加一个“if”以便编译后,我仍然收到以下代码的两个错误:
private void RightsizeZoomLevelForAllPushpins()
{
    try
    {
        // Set the view so that all pushpins are displayed, with a bit of a margin around them
        var map = this.userControl11.myMap;
        var locations = map.Children.OfType<Pushpin>().Select(x => x.Location);

        if (locations.Count() == 1)
        {
            map.setView(locations[0], singlePinZoom);                
        }
        else if (locations.Count() > 1) 
        {
            //Margin
            var w = new Pushpin().Width;
            var h = new Pushpin().Height;
            var margin = new Thickness(w / 2, h, w / 2, 0);

            //Set view
            map.SetView(locations, margin, 0);
        }    
        currentZoomLevel = Convert.ToInt32(map.ZoomLevel);
        UncheckAllZoomLevelToolstripMenuItems();
        SetZoomMenuItem();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}
他们是:
1>C:\Users\bclay\source\repos\MyMaps\MyMaps\Form1.cs(155,33,155,45): error CS0021: Cannot apply indexing with [] to an expression of type 'IEnumerable<Location>'
1>C:\Users\bclay\source\repos\MyMaps\MyMaps\Form1.cs(155,25,155,32): error CS1061: 'Map' does not contain a definition for 'setView' and no accessible extension method 'setView' accepting a first argument of type 'Map' could be found (are you missing a using directive or an assembly reference?)
顺便说一句,在编译项目之前,我在 Visual Studio 中看不到任何错误(它在输入错误时停止发现错误)。我不知道为什么......我没有改变任何设置......
更新 2
这是我现在使用的代码,它不只考虑一个图钉,但确实有效(请注意,“SetView”在此处不会引发异常):
private void RightsizeZoomLevelForAllPushpins()
    {
        const int LEFT_TOP_RIGHT_MARGIN_ADDITION = 16;
        const int BOTTOM_MARGIN_ADDITION = 48;
        try
        {
            // Set the view so that all pushpins are displayed, with a bit of a margin around them
            // from https://stackoverflow.com/questions/65779504/how-to-set-the-zoom-level-of-bing-map-to-just-wide-enough-to-display-all-pushpin/65781319#65781319
            var map = this.userControl11.myMap;
            var locations = map.Children.OfType<Pushpin>().Select(x => x.Location);
            //Margin
            var w = new Pushpin().Width;
            var h = new Pushpin().Height;
            var margin = new Thickness((w / 2) + LEFT_TOP_RIGHT_MARGIN_ADDITION, 
                                        h + LEFT_TOP_RIGHT_MARGIN_ADDITION, 
                                        (w / 2) + LEFT_TOP_RIGHT_MARGIN_ADDITION, 
                                        0 + BOTTOM_MARGIN_ADDITION);
            //Set view
            map.SetView(locations, margin, 0);
            currentZoomLevel = Convert.ToInt32(map.ZoomLevel);
            UncheckAllZoomLevelToolstripMenuItems();
            SetZoomMenuItem();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }

最佳答案

单个图钉没有单一的缩放级别,最佳缩放级别取决于您的场景或图钉的含义,以及 map 的大小,因为缩放和 map 大小都决定了可视区域。例如,如果您的图钉代表一所房子,您可能希望 map 放大得比代表城市时放大。
也就是说,这是您的代码的修改版本,它将处理单针场景并使用您选择的静态缩放级别:

private double singlePinZoom = 15;

private void RightsizeZoomLevelForAllPushpins()
{
    try
    {
        // Set the view so that all pushpins are displayed, with a bit of a margin around them
        var map = this.userControl11.myMap;
        var locations = map.Children.OfType<Pushpin>().Select(x => x.Location);

        if(locations.Count() > 0) {
            //Margin
            var w = new Pushpin().Width;
            var h = new Pushpin().Height;
            var margin = new Thickness(w / 2, h, w / 2, 0);

            //Set view
            map.SetView(locations, margin, 0);
        } else(locations.Count == 1) {
            map.setView(locations[0], singlePinZoom);
        }   
        
        currentZoomLevel = Convert.ToInt32(map.ZoomLevel);
        UncheckAllZoomLevelToolstripMenuItems();
        SetZoomMenuItem();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

关于c# - 当只有一个图钉时,如何确定 Bing map 的正确缩放级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66396195/

相关文章:

c# - 使用 AWS SDK 将文件上传到 .NET Core 中的 S3

javascript - ASP.NET 中的 CheckboxList 选中和取消选中

c# - 使用 .NET 进行 Java RSA/ECB/PKCS1Padding 加密

python - 添加对名称和命名空间中有点的 .net 程序集的引用

.net - 为自定义 TreeViewItem 自动生成名称

WPF VirtualizingStackPanel 以提高性能

c# - SelectMany 带有等待和结果排序

.net - 在 WinForms 中向系统菜单(左上角图标菜单)添加条目?

.net - 什么是最好的 .NET 游戏开发框架?

WPF IDATAError 在控件变为可见时不显示