6

I have a vector full of Lat Long coordinates in this format

82.0000000, -180.0000000

I am trying to convert them into an X and a Y coordinate to plot them to a map using this code, which as far as i can see is right...

X:

double testClass::getX(double lon)
{
    // Convert long to X coordinate (2043 = map width)
    double x =  lon;
    // Scale
    x =         x * 2043.0 / 360.0;
    // Center
    x +=        2043.0/2.0;
    return x;
}

Y:

double testClass::getY(double lat)
{
    // Convert lat to Y coordinate (1730 = map height)
    double y =  -1 * lat;
    // Scale
    y =         y * 1730.0 / 180.0;
    // Center
    y +=        1730.0/2.0;
    return y;
}

However when plotted on my map i can see the points do resemble a world map but they are all off by x amount and i think its something to do with my scaling

any ideas?

4

1 回答 1

7

好的,我找到了答案

double testClass::getX(double lon, int width)
{
    // width is map width
    double x = fmod((width*(180+lon)/360), (width +(width/2)));

    return x;
}

double testClass::getY(double lat, int height, int width)
{
    // height and width are map height and width
    double PI = 3.14159265359;
    double latRad = lat*PI/180;

    // get y value
    double mercN = log(tan((PI/4)+(latRad/2)));
    double y     = (height/2)-(width*mercN/(2*PI));
    return y;
}

所以是的,这在使用墨卡托地图时非常有效

于 2013-04-18T12:46:21.077 回答