I add a number of pushpins to a map and then "rightsize" or "right zoom" it so that all the pushpins show but so that the ones on the far edges are barely within the boundaries, like so:
However, if there is only one pushpin (as in the following case with Iowa, which only had the one civil war battle), rather than zero in on that as close as possible, it zooms out all the way to Skylab, like so:
This is the code I use to "right size" the 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);
}
}
How can I get a one-pushpin map to not only center, but also zoom in as far as possible?
UPDATE
After adding parens to both references to Count, and adding an "if" in the else block so that it would compile, I still get two errors with this code:
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);
}
}
They are:
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?)
BTW, I'm not seeing any errors anymore in Visual Studio until I compile the project (it stopped finding errors as they were entered). I don't know why ... I changed no settings...
UPDATE 2
This is the code I'm using now, which does not take just one pushpin into account, but does work (note that "SetView" doesn't throw an exception here):
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);
}
}