我正在尝试将此 C# 数学(我猜?)移植到 PHP:int mask = 1 << (i - 1);
这是该函数的完整 C# 代码:
/// <summary>
/// Converts tile XY coordinates into a QuadKey at a specified level of detail.
/// </summary>
/// <param name="tileX">Tile X coordinate.</param>
/// <param name="tileY">Tile Y coordinate.</param>
/// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
/// to 23 (highest detail).</param>
/// <returns>A string containing the QuadKey.</returns>
public static string TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
{
StringBuilder quadKey = new StringBuilder();
for (int i = levelOfDetail; i > 0; i--)
{
char digit = '0';
int mask = 1 << (i - 1);
if ((tileX & mask) != 0)
{
digit++;
}
if ((tileY & mask) != 0)
{
digit++;
digit++;
}
quadKey.Append(digit);
}
return quadKey.ToString();
}
这是我最终得到的 PHP 代码:
<?php
/*
* PHP class for converting Mercator X, Y and Z
* coordinates to Bing Maps Quad tree key
* Ported from C# code from MSDN
* http://msdn.microsoft.com/en-us/library/bb259689.aspx
*
* Usage:
* mercatorQuadkey(z, x, y)
* Example:
* mercatorQuadkey(15, 5240, 12661)
*/
function mercatorQuadkey($z, $x, $y){
for ($i = $levelOfDetail; $i > 0; $i--)
{
$digit = '0';
$mask = 1 << ($i - 1);
if ($tileX !== 0 && $mask !== 0)
{
$digit++;
}
if ($tileY !== 0 && $mask !== 0)
{
$digit++;
$digit++;
}
$quadkey = $quadkey.$digit;
}
return $quadkey;
}
?>
任何人都可以帮忙吗?我需要这个,因为我正在尝试在 Leaflet 中使用 Bing 地图,但我发现的所有插件都没有使用示例。顺便说一句,我对 C# 一无所知。