我想找出静态 map 上纬度/经度的像素坐标。例如,我从以下位置下载了图像:
我想要的是从纬度/经度长能够将该纬度映射到像素坐标。我搜索了一下,发现墨卡托投影可以解决我的问题。但是我找不到任何正确的方法来做到这一点。有人能帮帮我吗。另外,如 URL
所示,我已缩放至 9。
最佳答案
如果您想直接在 map 的位图上绘制,将谷歌静态 map 的纬度/经度转换为像素非常有用。这可能是比通过 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));
}
用法:
- 调用此函数获取 X 和 Y 像素坐标
- 结果引用位图的中心,因此添加 将宽度/2 和高度/2 位图映射到 x 和 y 值。这给你 绝对像素位置
- 检查像素位置是否位于位图中
- 画任何你想要的东西
由于 Google 的墨卡托投影变体,它无法在靠近两极的地方工作,但对于通常的坐标,它工作得很好。
关于java - 从谷歌静态 map 获取像素坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23898964/