简体   繁体   English

如何使用PHP将Bing映射四键转换为缩放级别,x坐标和y坐标?

[英]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.

相关问题 如何根据每个点的 X 坐标或 Y 坐标将点列表划分为列表? - how to divide list of points into lists based on each point's X-coordinate or Y-coordinate? 如何计算要缩放的X / Y坐标 - How can you calculate the X/Y coordinate to zoom to 如何检查玩家在 C# 中低于 y 坐标的时间 - How to Check the time a player is below a y-Coordinate in C# 鼠标Y坐标与我期望的相反 - Mouse Y-Coordinate is opposite from what I'm expecting 错误的坐标点bing地图 - Wrong coordinate points bing maps 给定一系列位置,如何确定Bing地图的适当缩放级别和地图中心? - Given a collection of Locations, how can I determine the appropriate Zoom level and Map Center for Bing Maps? 当只有一个图钉时,如何确定 Bing 地图的正确缩放级别? - How can I determine the correct zoom level for Bing Maps when there is only one pushpin? 如何使用 x 和 y 坐标数组绘制矩形轮廓? - How to draw rectangular profiles using x and y coordinate array? 如何使用鼠标坐标,平移和缩放比例确定原始坐标? - How to determine the original coordinate using the mouse coordinate, the translation and the zoom scale? 如何从图像中找到x,y坐标? - How to find x,y coordinate from an image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM