我想找出静态地图上lat / lng的像素坐标。例如,我从以下网站下载了图片:
我想要的是从lat / lng long能够将latlng映射到像素坐标。我搜索了一下,发现墨卡托投影可以解决我的问题。但是我找不到任何正确的方法。有人能帮帮我吗。此外,我已经缩放到9,如URL
所示。
答案 0 :(得分:2)
如果要直接在地图的位图上绘制,将Lat / Lon转换为Google静态地图的像素非常有用。这比使用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 :(得分:0)
harry4616's code in 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))