# How to convert a bing maps quadkey to mercator coordinates

How do you convert a bing maps quad key, such as "03200320023" to coordinates in mercator virtual earth (epsg 3857) coordinate system?

I've got an algorithm to convert to coordinates in latitude longitude (e.g. the quad key above comes to "-84.19921875,33.7243385314941,-84.0234375,33.8704147338867"). I could then convert from latitude longitude to mercator coordinate, but that smells far too expensive in processing power.

The mercator virtual earth result I'm looking for given the above quadkey is: "-9373014,4011415,-9353446,4030983"

Also a question on stackoverflow

-
Quadkeys aren't a coordinate reference system. Are you trying to get the center of a quad? – Sean Apr 27 '11 at 17:50
Have you reviewed Microsoft's Bing tiling scheme documentation ? – Kirk Kuykendall Apr 27 '11 at 18:36
I've read all the bing maps docs. All I want to do is calculate the extents for a quadkey in epsg:3857 so I can pass a web request to mapserver for the location. – Nick R Apr 27 '11 at 18:57

The code below comes close. Maybe this can get you started. Update: notice the amount of error equals the last offset size.

``````quadkey: 03200320023
0 offset: 10018754.1713946 lod 0
3 offset: 5009377.08569731 lod 1
2 offset: 2504688.54284866 lod 2
0 offset: 1252344.27142433 lod 3
0 offset: 626172.135712164 lod 4
3 offset: 313086.067856082 lod 5
2 offset: 156543.033928041 lod 6
0 offset: 78271.5169640205 lod 7
0 offset: 39135.7584820102 lod 8
2 offset: 19567.8792410051 lod 9
3 offset: 9783.93962050256 lod 10
Mercator: x -9363230.21682095, y 4001631.30478555
error x 9783.78317905217 y-9783.69521445315
``````

Here's the code:

``````private void BingTest()
{

string qdkey = "03200320023";
double x = 0;
double y = 0;
double offset = EarthRadius * Math.PI / 2.0;
for (int i = 0; i < qdkey.Length; i++)
{
string s = qdkey.Substring(i, 1);
Debug.Print("{0} offset: {1} lod {2}",s, offset,i);
switch (s)
{
case "0":  x -= offset; y += offset; break;
case "1":  x += offset; y += offset; break;
case "2":  x -= offset; y -= offset; break;
case "3":  x += offset; y -= offset; break;
}
offset *= 0.5;
}

// compare with expected result
double x0 = -9373014;
double y0 = 4011415;
Debug.Print("Mercator: x {0}, y {1}", x, y);
Debug.Print("error x {0} y{1}", x - x0, y - y0);
}
``````
-
Superb - thank you! – Nick R Apr 28 '11 at 5:50

Take a look at tilebelt https://github.com/mapbox/tilebelt :

``````quadkeyToTile(quadkey)
``````
-

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.