问题描述
我在地图上添加了一些图钉,然后“调整大小”或“正确缩放”它,以便显示所有图钉,但使远边缘的图钉几乎不在边界内,如下所示:
然而,如果只有一个图钉(如爱荷华州的以下案例,它只有一场内战),而不是尽可能接近零,它会一直缩小到 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,0);
//Set view
map.SetView(locations,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,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,0);
currentZoomLevel = Convert.ToInt32(map.ZoomLevel);
UncheckAllZoomLevelToolstripMenuItems();
SetZoomMenuItem();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)