This project has moved. For the latest updates, please go here.

Is there a way to specify a bounding box for view?

May 13, 2013 at 12:41 PM
I have a bounding box of two coordinates. Is it possible to change the viewport to exactly this boundingbox (Center & Zoom)?

May 13, 2013 at 1:43 PM
Well you can zoom in so that the map level is suitable to contain both points.

Here is an extract of the code I use to do this.. its part of the code that allows you to draw a rectangle on the screen and zoom in..

call ZoomToExtent with appropriate coordinates.
    /// <summary>
    /// calculates a suitable zoom level given a boundary
    /// </summary>
    /// <param name="bounds"></param>
    /// <returns></returns>
    private double GetBoundsZoomLevel(double north, double east, double south, double west)
        var GLOBE_HEIGHT = 256; // Height of a map that displays the entire world when zoomed all the way out
        var GLOBE_WIDTH = 256; // Width of a map that displays the entire world when zoomed all the way out

        var latAngle = north - south;
        if (latAngle < 0)
            latAngle += 360;

        var lngAngle = east - west;

        var latZoomLevel = Math.Floor(Math.Log(map.RenderSize.Height * 360 / latAngle / GLOBE_HEIGHT) / Math.Log(2));
        var lngZoomLevel = Math.Floor(Math.Log(map.RenderSize.Width * 360 / lngAngle / GLOBE_WIDTH) / Math.Log(2)); //0.6931471805599453 

        return (latZoomLevel < lngZoomLevel) ? latZoomLevel : lngZoomLevel;

    /// <summary>
    /// Zoom to most appropriate level to encompass the given rectangle
    /// </summary>
    /// <param name="north"></param>
    /// <param name="east"></param>
    /// <param name="south"></param>
    /// <param name="west"></param>
    public void ZoomToExtent(double north, double east, double south, double west)
        double zoom = GetBoundsZoomLevel(north, east, south, west);
        double cx = west + (east - west) / 2;
        double cy = south + (north - south) / 2;

        zoom = Math.Floor(zoom) - 0.5;

        map.TargetCenter = new Location(cy, cx);
        map.TargetZoomLevel = zoom;
May 13, 2013 at 3:40 PM
Edited May 13, 2013 at 3:40 PM
Andreas, if the question really is whether it is possible to exactly match the bounding box, the answer is generally No. An exact match would require to also fit the viewport's aspect ratio to that of the bounding box, which would either change the map control's width or height.
May 13, 2013 at 5:03 PM
Well it is possible to exactly match - but probably not wise - hence the compromise. Andreasbalzer, out of curiosity why does it have it match exactly?
May 13, 2013 at 6:13 PM
thanks for your answers. I develop an indoor editor for OpenStreetMap. I let the user pan around to set a bounding box being used to download information of OSM. As the user can go back and forth between several tasks, it would be bad if the bounding box changes too much. At the moment the user cannot transfer files between computers and the size is more or less locked at this time. The code provided will work just fine for now. Thanks :-)