zic10
zic10

Reputation: 2330

Bing Map Zoom Out Stretch

I'm working in a WPF application using a Bing Map. One thing that is bothering me is if you maximize the window and zoom out with the mouse wheel, you can zoom back far enough to the point where the map is smaller in height then the bounds of the container that it is in. I've looked through the documentation to see if there is a way to limit this. I understand that I can subscribe to the zoom in/out event changes and limit the ZoomLevel. (See the attached image which shows the issue I'm having). What I want to know is if there is a property or another means either in the XAML or on the map to always ensure that the map can fill up the container no matter what the window size regardless of zoom level. I've tried setting the HorizontalAlignment="Stretch" VerticalAlignment="Stretch" but this still does not solve that zoom out problem. Any help would be appreciated.

 xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
 <m:Map x:Name="MapControl"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"
                   Center="{Binding CenterLocation,
                                    Mode=TwoWay,
                                    UpdateSourceTrigger=Explicit}"
                   CredentialsProvider="MAP_KEY"
                   ZoomLevel="{Binding ZoomLevel}">
                <m:Map.Children>
                    <TextBlock FontSize="20"
                               Foreground="Blue"
                               Text="{Binding ReportText}" />
                    <m:MapItemsControl ItemTemplate="{StaticResource PinTemplate}" ItemsSource="{Binding WeatherItemSource}" />
                </m:Map.Children>
            </m:Map>

And here is the image with the gray edges:

enter image description here

Upvotes: 2

Views: 946

Answers (1)

rbrundritt
rbrundritt

Reputation: 17954

There isn't a property to limit this, but you can easily calculate which zoom level is needed to fill the map container. At zoom level 1, the height of the world map (not to be confused with the map container) is 512 pixels. This doubles with each zoom level. Zoom level 2 would have a height of 1024 pixels and so on. Zoom level 3 would have a height of 2048 pixels and so on.

4K screens have a vertical resolution of around 2000 pixels. With this in mind you likely only need to do a simple check when the map is loaded and resized to determine what zoom level to limit the map to.

if(mapHeight < 512){
    minZoom = 1;
} else if(mapHeight < 1024){
    minZoom = 2;
} else if(mapHeight < 2048){
    minZoom = 3;
} else{
    minZoom = 4;
}

If you want to have a more elegant solution for calculating the zoom level for any possible screen size, you can do this:

minZoom = Math.Max(Math.Floor(Math.Log(mapHeight / 256, 2)) + 1, 1);

Upvotes: 1

Related Questions