【发布时间】:2021-05-29 10:43:22
【问题描述】:
我在地图上添加了一些图钉,然后“调整大小”或“右缩放”它,以便所有图钉都显示出来,但远边缘的图钉几乎不在边界内,如下所示:
但是,如果只有一个图钉(如下面的爱荷华州的例子,它只有一场内战),而不是尽可能接近零,它会一直缩小到 Skylab,像这样:
这是我用来“调整大小”地图的代码:
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);
}
}
如何让一键式地图不仅居中而且尽可能放大?
更新
在对 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);
}
}
【问题讨论】:
-
对您来说“可能”有多远?对我来说,这是“一直到放大按钮被禁用”
-
@CaiusJard “这样所有的图钉都会显示出来,但远边缘的图钉几乎不会在边界内”
-
但如果只有一个图钉..
-
你已经学习了关于
SetView的单点here,所以你可以检查位置是否有单点,使用通过locations.First()接受单个位置的重载,否则通过传递locations使用使用 IEnumerable 位置的重载。选择单个点的缩放级别由您决定。 -
下面的答案分享了这个想法,但不幸的是有一些语法错误而且 if/else 的逻辑是错误的。要检查我提到的计数,你需要使用
locations.Count(),以获得第一,你不能使用[],你需要使用locations.First(),条件应该是locations.Count() ==1,locations.Count() >1跨度>
标签: c# .net wpf winforms bing-maps