[英]How can I convert a Bing maps quadkey into zoom level, x-coordinate and y-coordinate using PHP?
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. 我正在尝试用PHP编写一个脚本,该脚本将接受Bing地图四键,然后返回缩放级别,x坐标和y坐标,以便可以使用自己的地图。 I've based my code off the C# example code provided by Microsoft as such here :
我根据我的代码关闭微软提供这样的C#示例代码在这里 :
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 > 0; i--)
{
int mask = 1 << (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.");
}
}
}
This is my recreation using PHP that isn't operating as I would expect: 这是我使用PHP进行的娱乐,它无法正常运行:
$quadkey = intval($_GET["quadkey"]);
$zoom = count($quadkey);
for ($i = $zoom; $i > 0; $i--)
{
$mask = 1 << ($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";
}
The example quadkey I'm using and the expected results are as follows: 我正在使用的示例四键和预期结果如下:
Quadkey: 120202111102203112
X-coord: 134926
Y-coord: 86121
Zoom: 18
四键:
120202111102203112
X坐标: 134926
Y坐标: 86121
缩放: 18
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!
谢谢大家!
There are mistakes in my code that I should have seen. 我的代码中有我应该看到的错误。
$zoom = count($quadkey);
Is wrong and counts the number of $quadkeys, not the length of the string. 是错误的,它计算$ quadkeys的数目,而不是字符串的长度。
$zoom = strlen($quadkey);
Also, the URL should be generated outside of the for loop, just below it. 同样,URL应该在for循环的外部,紧靠其下方生成。 I have placed the updated code below should anyone else need a PHP script for converting a Bing maps quadkey to coordinates.
如果其他任何人需要将Bing maps四键转换为坐标的PHP脚本,则将更新后的代码放在下面。
$quadkey = intval($_GET["quadkey"]);
$zoom = strlen($quadkey);
for ($i = $zoom; $i > 0; $i--)
{
$mask = 1 << ($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";
With Bing Ajax 7 the QuadKey in not the only item provided, it also provides x, y, and levelOfDetail, aka Zoom. 使用Bing Ajax 7时,不仅提供了QuadKey,还提供了x,y和levelOfDetail,也就是Zoom。
Until I spotted this I too thought about a server side transformation, but it is available to you in javascript. 在我发现这一点之前,我还考虑过服务器端转换,但是您可以在javascript中使用它。
Bing maps comes with x,y, and z that can be used for OSM web requests directly: 必应地图随附x,y和z,可直接用于OSM Web请求:
var map = new Microsoft.Maps.Map( <snip> )
function useZXY(tile) {
return "http://tile.openstreetmap.org/" +
tile.levelOfDetail + "/" +
tile.x + "/" +
tile.y +".png";
}
var omsTS = new Microsoft.Maps.TileSource({ uriConstructor: useZXY });
var omsTL = new Microsoft.Maps.TileLayer({ mercator: omsTS, opacity: 0.5 });
map.entities.push(omsTL) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.