在c++中将长坐标转换为Y坐标

Convert lat long to X Y coordinates C++

本文关键字:坐标 转换 c++      更新时间:2023-10-16

我有一个充满Lat Long坐标的向量,格式为

82.0000000, -180.0000000

我正试图将它们转换为X和Y坐标,以使用此代码将它们绘制成地图,据我所知,这是正确的…

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;
}

然而,当绘制在我的地图上时,我可以看到这些点确实像一个世界地图,但它们都偏离了x量,我认为这与我的缩放有关

任何想法?

我找到答案了

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;
}

所以当使用墨卡托地图