Question

I'm trying to set the zoom level of Bing Map to slightly further back than the LocationRect. I looked around a bit and couldn't find anything about it. This is the code to set the view of the map

myMap.SetView(new LocationRect(locationCollection));

as used in Windows 8 App XAML/C#: Set multiple pushpins to Bing map in one method

Was it helpful?

Solution

I've come across this a couple of times. Usually people create a LocationRect from a collection of locations used by pushpins and then set the view to find that some of the pushpins are out of view. This is because the LocationRect does not take into consideration the pixel size of the pushpins and only focuses on the coordinate. In V7 we have an option to add a padding but this same option isn't in the Windows Store SDK. I wrote a blog post a while back on how to calculate the best map view based on a collection of locations with a buffer (). Using this I put together this reusable method for calculating the zoom level for a bounding box with a buffer. You can then use this zoom level and the center property of the LocationRect to set the map view.

public double CalculateZoomLevel(LocationRect boundingBox, double buffer, Map map)
{
    double zoom1=0, zoom2=0; 

    //best zoom level based on map width
    zoom1 = Math.Log(360.0 / 256.0 * (map.ActualWidth - 2*buffer) / boundingBox.Width) / Math.Log(2);

    //best zoom level based on map height
    zoom2 = Math.Log(180.0 / 256.0 * (map.ActualHeight - 2*buffer) / boundingBox.Height) / Math.Log(2);

    //use the most zoomed out of the two zoom levels
    var zoomLevel = (zoom1 < zoom2) ? zoom1 : zoom2;

    return zoomLevel;
}

OTHER TIPS

Unfortunately, as far as I know, there is no 'padding' option like we could have in AJAX v7.

One possible to do what you're trying to do is to add fake locations is this LocationCollection so you can manually extend the area (you can use the zoom level and position to calculated the map resolution to have a better positioning of those fake location)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top