我在地图上添加了许多图钉,然后通过“rightsize”或“right zoom”来调整地图的大小,以便所有的图钉都能显示出来,但是边缘上的图钉仅在边界内露出一点,就像这样:
。但是,如果只有一个图钉(例如艾奥瓦州的情况,只有一个内战战斗),它不会尽可能地放大该图钉,而是一直缩小到Skylab,就像这样:
。这是我用来“调整地图大小”的代码:
![enter image description here](https://istack.dev59.com/vbk0f.webp)
![enter image description here](https://istack.dev59.com/IVyZh.webp)
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);
}
}
SetView
,可以在此处查看(https://stackoverflow.com/a/65513589/3110834),因此你可以检查位置中是否有单个点,通过传递locations.First()
来使用接受单个位置的重载,否则,通过传递locations
使用使用位置的IEnumerable的重载。选择单个点的缩放级别由你决定。 - Reza Aghaeilocations.Count()
,要获取第一个,您不能使用[]
,您需要使用locations.First()
,此外条件应该是locations.Count() ==1
,locations.Count() >1
。 - Reza Aghaei