0

我想在静态地图上找出 lat/lng 的像素坐标。例如,我从以下位置下载了一张图片:

链接到图片

我想要的是从 lat/lng long 能够将该 latlng 映射到像素坐标。我搜索了一下,发现墨卡托投影可以解决我的问题。但是我找不到任何合适的方法。有人能帮帮我吗。此外,如图所示,我已放大到 9 URL

4

2 回答 2

2

如果您想直接在地图的位图上绘制,将谷歌静态地图的纬度/经度转换为像素非常有用。这可能是比使用 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. 随心所欲地画

由于谷歌的墨卡托投影变体,它不能在两极附近工作,但对于通常的坐标,它工作得很好。

于 2014-12-05T01:13:02.980 回答
0

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))
于 2016-04-21T22:27:05.117 回答