How can I determine the correct zoom level for Bing Maps when there is only one pushpin?

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:

enter image description here

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:

enter image description here

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?

1 answer

  • answered 2021-02-27 19:37 rbrundritt

    There is no single zoom level for a single pin, the best zoom level would depend on your scenario or what the pin is meant to represent, as well as the size of the map since both zoom and map size determine the viewable area. For example, if your pin represented a house you may want the map to be zoomed in more than if it represented a city.

    That said, here is a modified version of your code that will handle the single pin scenario and use a static zoom level of your choosing:

    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);
        }
    }