从谷歌静态地图获得像素协调



我想找出静态地图上lat/lng的像素坐标。例如,我从

下载了一个图像:

链接到图片

我想要的是从lat/lng长能够映射到像素坐标。我搜索了一下,发现墨卡托投影可以解决我的问题。然而,我找不到任何合适的方法来做这件事。有人能帮帮我吗?此外,我已经放大到9时,如URL .

如果你想直接在地图的位图上绘制,将Lat/Lon转换为像素对于谷歌静态地图是非常有用的。这可能是比在URL中传递数百个参数更好的方法。我遇到了同样的问题,并在网上找到了四个解决方案,它们看起来非常相似,但都是用其他语言写的。我把它翻译成c#。我相信在Java或C中也可以很容易地使用这个简单的代码:

//(half of the earth circumference's in pixels at zoom level 21)
static double offset = 268435456; 
static double radius = offset / Math.PI;
// X,Y ... location in degrees
// xcenter,ycenter ... center of the map in degrees (same value as in 
// the google static maps URL)
// zoomlevel (same value as in the google static maps URL)
// xr, yr and the returned Point ... position of X,Y in pixels relativ 
// to the center of the bitmap
static Point Adjust(double X, double Y, double xcenter, double ycenter, 
                    int zoomlevel)
{
    int xr = (LToX(X) - LToX(xcenter)) >> (21 - zoomlevel);
    int yr = (LToY(Y) - LToY(ycenter)) >> (21 - zoomlevel);
    Point p = new Point(xr, yr);
    return p;
}
static int LToX(double x)
{
    return (int)(Math.Round(offset + radius * x * Math.PI / 180));
}
static int LToY(double y)
{
    return (int)(Math.Round(offset - radius * Math.Log((1 + 
                 Math.Sin(y * Math.PI / 180)) / (1 - Math.Sin(y * 
                 Math.PI / 180))) / 2));
}

用法:

  1. 调用这个函数来获取X和Y像素坐标
  2. 的结果被引用到位图的中心,所以添加位图宽度/2和高度/2到x和y值。这给了你绝对像素位置
  3. 检查像素位置是否在位图内
  4. 想画什么就画什么

由于Google的墨卡托投影,它不能在靠近极点的地方工作,但对于通常的坐标,它工作得很好。

harry4616的python代码:

import math
OFFSET = 268435456 # half of the earth circumference's in pixels at zoom level 21
RADIUS = OFFSET / math.pi
def get_pixel(x, y, x_center, y_center, zoom_level):
    """
    x, y - location in degrees
    x_center, y_center - center of the map in degrees (same value as in the google static maps URL)
    zoom_level - same value as in the google static maps URL
    x_ret, y_ret - position of x, y in pixels relative to the center of the bitmap
    """
    x_ret = (l_to_x(x) - l_to_x(x_center)) >> (21 - zoom_level)
    y_ret = (l_to_y(y) - l_to_y(y_center)) >> (21 - zoom_level)
    return x_ret, y_ret
def l_to_x(x):
    return int(round(OFFSET + RADIUS * x * math.pi / 180))
def l_to_y(y):
    return int(round(OFFSET - RADIUS * math.log((1 + math.sin(y * math.pi / 180)) / (1 - math.sin(y * math.pi / 180))) / 2))

最新更新