给定缩放级别,将长/ lat转换为像素x/y

Jon*_*han 7 .net maps google-maps pixel coordinates

我正在尝试在ASP.NET中开发一个页面,它将充当Google Map的tile-server

它将从数据库中提取一组纬度/经度点,然后在给定缩放级别(默认值:15)的情况下将它们渲染为透明背景上的小红点.

然后它将结果作为GIF类型的图像返回.

是否开发了任何算法或库,允许我采用这组纬度/经度并将其转换为一组2D像素坐标,给定缩放级别?

(这一切都是在服务器端完成的,因此我无法使用Google Maps API.)


更新:在Perl中找到了类似的代码示例:

http://blog.barros.ws/2009/03/06/convert-lat-lng-and-zoom-values-to-pixel-xy-on-a-map/

麻烦的是,我不知道Perl,也没有时间破解书籍并学习它.

任何人都可以帮我解读这个函数中发生的事情吗?

sub Google_Coord_to_Pix
{
    my $value   = shift ;
    my $lat = shift ;
    my $lng = shift ;
    my @d       = ( ) ; 
    my $e       = 0 ;

    $d[1] = sprintf("%0.0f", $$value{'bmO'} + $lng * $$value{'pixLngDeg'} ) ;

    $e = sin($lat * $$value{'Wa'}) ;

    if( $e > 0.99999 )
    {
        $e = 0.99999 ;
    }

    if( $e < -0.99999 )
    {
        $e = -0.99999 ;
    }

    $d[0] = sprintf("%0.0f", $$value{'bmO'} + 0.5 * log((1 + $e) / (1 - $e)) * (-1) * $$value{'pixLngRad'} ) ;

    return (@d) ;
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*s B 9

这是我正在使用的一些代码.它是在PHP中.

// Returns longitude in pixels at a certain zoom level
function lonToX($lon, $zoom) {
    $offset = 256 << ($zoom-1);
    return round($offset + ($offset * $lon / 180));
}
// Returns latitude in pixels at a certain zoom level
function latToY($lat, $zoom) {
    $offset = 256 << ($zoom-1);
    return round($offset - $offset/pi() * log((1 + sin($lat * pi() / 180)) / (1 - sin($lat * pi() / 180))) / 2);
}
Run Code Online (Sandbox Code Playgroud)

基于此页面的代码,由这个人写的.

祝好运!

更新: 此地图是帮助了解图块在Google地图中的工作方式的绝佳方式

编辑:这是VB.NET中的一组等效函数:

Public Function LonToX(Lon As Double, Zoom as UInteger) As UInteger
    Dim Offset = 256 << (Zoom - 1)
    Return Math.Round(Offset + (Offset * Lon / 180))
End Function

Public Function LatToY(Lat As Double, Zoom as UInteger) As UInteger
    Dim Offset = 256 << (Zoom - 1)
    Return Math.Round(Offset - Offset / Math.Pi * Math.Log((1 + Math.Sin(Lat * Math.Pi / 180)) / (1 - Math.Sin(Lat * Math.Pi / 180))) / 2)
End Function
Run Code Online (Sandbox Code Playgroud)

在C#中:

public uint lonToX(double lon, uint zoom) {
    uint offset = 256 << (zoom - 1);
    return Math.Round(offset + (offset * lon / 180));
}

public uint latToY(double lat, uint zoom) {
    uint offset = 256 << (zoom - 1);
    return Math.Round(offset - offset / Math.Pi * Math.Log((1 + Math.Sin(lat * Math.Pi / 180)) / (1 - Math.Sin(lat * Math.Pi / 180))) / 2);
}
Run Code Online (Sandbox Code Playgroud)


lgu*_*guy 2

“如果是墨卡托投影,则无需担心地球的曲率,因为所有纬度/经度线的间距相等”

\n\n

也许您正在考虑地理(又名 Plate Carree)投影?墨卡托投影确实具有等距经度线,但没有等距纬度线(lat = atan(sinh(y)),因此 90\xc2\xb0 处于无穷大)。

\n\n

顺便说一句,球体上墨卡托投影的数学计算在这里,但如果 Google 地图使用 WGS84 椭球体并且您需要精确计算,它会变得更加复杂。在这种情况下,我会看看这个,但要注意:它不适合胆小的人。

\n