从Google静态地图获取像素协调 [英] Getting pixel coordinated from google static maps
问题描述
我想找出静态地图上经纬度的像素坐标.例如,我从以下位置下载了图像:
I want to find out the pixels coordinates of a lat/lng on an static map. For example i've downloaded an image from:
我想要的是纬度/经度,以便能够将纬度映射到像素坐标.我搜索了一下,发现墨卡托投影可以解决我的问题.但是我找不到任何合适的方法.有人能帮帮我吗.如URL
所示,我也放大到了9.
What I want is from a lat/lng long to be able to map that latlng to pixel coordinates. I've searched a bit and found that mercator projection can solve my problem. However I could not find any proper way of doing it. Can somebody please help me. Also I've zoomed to 9 when as shown in the URL
.
推荐答案
如果要直接在地图的位图上绘制,则将lat/Lon转换为google静态地图的像素非常有用.与通过URL传递数百个参数相比,这是一种更好的方法.我遇到了同样的问题,并在网上找到了四个解决方案,它们看起来非常相似,但是用其他语言编写.我将其翻译成C#.我敢肯定,在Java或C语言中也可以轻松使用此简单代码:
Converting Lat/Lon to pixel for a google static map is very useful if you want to draw on the map's bitmap directly. This can be a better approach than passing hundreds of parameters with the URL. I had the same problem and found four solutions in the web which looked very similar but where written in other languages. I translated it into C#. I am sure it will be easy to use this simple code also in Java or 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像素坐标
- 结果参考到位图的中心,因此添加 位图将width/2和heigth/2映射到x和y值.这给你 绝对像素位置
- 检查像素位置是否在您的位图之内
- 随心所欲画
- call this function to get the X and Y pixel coordinates
- the result is referenced to the center of the bitmap, so add the bitmaps width/2 and heigth/2 to the x and y value. This gives you the absolute pixel position
- check if the pixel position is inside your bitmap
- draw whatever you want
由于Google墨卡托投影的变体,它无法在极点附近工作,但是对于通常的坐标来说,效果很好.
It does not work close to the poles because of Google's variant of Mercator projection, but for the usual coordinates it works very well.
这篇关于从Google静态地图获取像素协调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!