百度地图坐标与GPS坐标的转换

2018-3-25 C++

参考各种坐标的转换:https://blog.csdn.net/doulejian/article/details/70155080

GPS坐标在百度地图上显示位置:http://developer.baidu.com/map/jsdemo.htm#a5_2

获取百度地图坐标点:http://api.map.baidu.com/lbsapi/getpoint/index.html

#pragma once

static const double PI = 3.1415926535897932384626;
static const double A = 6378245.0;
static const double EE = 0.00669342162296594323;
class Gps
{
public:
Gps(void){}
~Gps(void){}
Gps(double wgLat, double wgLon)
{
SetWgLat(wgLat);
SetWgLon(wgLon);
}

double GetWgLat()
{
return m_wgLat;
}

void SetWgLat(double wgLat)
{
m_wgLat = wgLat;
}

double GetWgLon()
{
return m_wgLon;
}

void SetWgLon(double wgLon)
{
m_wgLon = wgLon;
}

Gps Transform(double lat, double lon)
{
if (TRUE == OutOfChina(lat, lon))
{
return Gps(lat, lon);
}

double dLat = TransformLat(lon - 105.0, lat - 35.0);
double dLon = TransformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * PI;
double magic = sin(radLat);
magic = 1 - EE * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (A / sqrtMagic * cos(radLat) * PI);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return Gps(mgLat, mgLon);
}

double TransformLat(double x, double y)
{
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
+ 0.2 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * PI) + 20.0 * sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * sin(y * PI) + 40.0 * sin(y / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * PI) + 320 * sin(y * PI / 30.0)) * 2.0 / 3.0;
return ret;
}

double TransformLon(double x, double y)
{
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y
+ 0.1 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * PI) + 20.0 * sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * sin(x * PI) + 40.0 * sin(x / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * PI) + 300.0 * sin(x / 30.0 * PI)) 
* 2.0 / 3.0;
return ret;
}

BOOL OutOfChina(double lat, double lon) 
{
if (lon < 72.004 || lon > 137.8347)
{
return TRUE;
}
if (lat < 0.8293 || lat > 55.8271)
{
return TRUE;
}
return FALSE;
}

Gps Bd09ToGcj02(double bdLat, double bdLon)
{
double x = bdLon - 0.0065;
double y = bdLat - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * PI);
double theta = atan2(y, x) - 0.000003 * cos(x * PI);
double ggLon = z * cos(theta);
double ggLat = z * sin(theta);
return Gps(ggLat, ggLon);

}

Gps Gcj02ToGps84(double bdLat, double bdLon)
{
Gps gps = Transform(bdLat, bdLon);
double lon = bdLon * 2 - gps.GetWgLon();
double lat = bdLat * 2 - gps.GetWgLat();
return Gps(lat, lon);
}

Gps Bd09ToGps84()
{
Gps gcj02 = Bd09ToGcj02(m_wgLat, m_wgLon);
Gps map84 = Gcj02ToGps84(gcj02.GetWgLat(), gcj02.GetWgLon());
return map84;
}

private:
double m_wgLat;
double m_wgLon;
};


标签: 百度坐标 GPS

评论(0) 浏览(1336)

Powered by EMLOG Copyright @ 狼酒 版权所有. 闽ICP备14012694号-2