Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I convert a Bing maps quadkey into zoom level, x-coordinate and y-coordinate using PHP?
    text
    copied!<p>I'm trying to write a script in PHP that will accept a Bing maps quadkey and then return the zoom level, x-coordinate and y-coordinate so that I can use my own maps. I've based my code off the C# example code provided by Microsoft as such <a href="http://msdn.microsoft.com/en-us/library/bb259689.aspx" rel="nofollow">here</a>:</p> <pre><code>public static void QuadKeyToTileXY(string quadKey, out int tileX, out int tileY, out int levelOfDetail) { tileX = tileY = 0; levelOfDetail = quadKey.Length; for (int i = levelOfDetail; i &gt; 0; i--) { int mask = 1 &lt;&lt; (i - 1); switch (quadKey[levelOfDetail - i]) { case '0': break; case '1': tileX |= mask; break; case '2': tileY |= mask; break; case '3': tileX |= mask; tileY |= mask; break; default: throw new ArgumentException("Invalid QuadKey digit sequence."); } } } </code></pre> <p>This is my recreation using PHP that isn't operating as I would expect:</p> <pre><code>$quadkey = intval($_GET["quadkey"]); $zoom = count($quadkey); for ($i = $zoom; $i &gt; 0; $i--) { $mask = 1 &lt;&lt; ($i - 1); $quadkey_array = str_split($quadkey); switch ($quadkey_array[$zoom - $i]) { case 0: break; case 1: $x |= $mask; break; case 2: $y |= $mask; break; case 3: $x |= $mask; $y |= $mask; break; default: echo "Error"; } echo "/" . $zoom . "/" . $x . "/" . $y . ".png"; } </code></pre> <p>The example quadkey I'm using and the expected results are as follows:</p> <p>Quadkey: <code>120202111102203112</code> X-coord: <code>134926</code> Y-coord: <code>86121</code> Zoom: <code>18</code></p> <p>Would anyone be able to shed some light on what I'm doing wrong? I've been looking all around and can't find any other example code to examine! Thanks all!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload