How do I find correct Latitude-Values for a given pixel-rectangle?

Hi there,

I hope anyone can answer my following question which I just can’t answer for many days now by myself.

I do have an MFC-application with a client window, let’s say 700 pixel wide and 500 pixel in height,
this may differ dynamically as the user resizes the window.


| |
| |
| | 500

       700

Now I try to draw the OSM-map in the background with the help of a center position and
a width and height parameter in pixel.

Let’s say on the left side I have a Longitude_1 and on the right side I have a Longitude_2,
the lower bound should be of Latitude_1.

######################################
THE BIG QUESTION IS NOW:

How do I calculate now Latitude_2 in the given zoom level?
######################################

If I understand it right, when I get a bitmap, e.g. from http://ojw.dev.openstreetmap.org/StaticMap/
or others the left side has the same longitude in the lower and upper corner as well as the right side does,
is this correct?

Now I try to find the zoom level which is closest to my window size, that means:

for (uiLauf = 1; uiLauf < 17; uiLauf++)
{
// Compute tiles
// 2^uiLauf - 1
uiZoomT = (uiZoomT + 1) * 2 - 1;
uiZoomN = (uiZoomN + 1) * 2 - 1;

uiWidthT  = (unsigned int) (1.0 * (Longitude_2 - Longitude_1) * 256 / 360 * uiZoomT);
uiWidthN  = (unsigned int) (1.0 * (Longitude_2 - Longitude_1) * 256 / 360 * uiZoomN);

if ((uiWidthT <= Rect.Width()) &&
    (uiWidthN >= Rect.Width())    )
{
  break;
}

}

Rect.Width() is the width of the client rectangle, in my upper example it is 700.

Therefore I try to find a zoom level which is in pixel as close as possible to the pixel-width of my client window.

At the end I fit the image directly in my window.

However I don’t know how to set the height value in pixel and afterwards how to calculate the correct upper and
lower latitude values of the whole bitmap for further use in my application.

Is there any formular that I could use to determine the correct latitude values?

I’m really desperate right now and would really appreciate any concrete help you can get me!

Thank you very very very much in advance for your time and effort!

Bye bye,

Roland