问题
I want to find out the pixels coordinates of a lat/lng on an static map. For example i've downloaded an image from:
Link to Image
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
.
回答1:
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));
}
Usage:
- 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
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.
回答2:
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))
来源:https://stackoverflow.com/questions/23898964/getting-pixel-coordinated-from-google-static-maps