标准地球坐标即GPS设备获得的坐标,该坐标需要经过国家测绘局进行加密后形成火星坐标(WGS-84 ),我们用的google坐标和高德地图坐标也就是火星坐标
百度地图,在火星坐标的基础上再进行一次加密,形成了百度地图上的坐标,因此,直接将标准地球坐标显示在百度地图上是会有几百米的偏差的。按照此原理,标准GPS坐标经过两步的转换可得到百度坐标。因为在处理百度地图时,例如查询其POI都需要百度地图上的坐标,而不是标准坐标,那么这样的转换就是必要的了。下面是两步转换的C++ 程序,供开发的人做一个参考。
//============================================================================ // Name : Convert_cpp.cpp // Author : roger // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <math.h> #include <stdlib.h> #include <iomanip> using namespace std; const double pi = 3.14159265358979324; const double a = 6378245.0; const double ee = 0.00669342162296594323; const double x_pi = 3.14159265358979324 * 3000.0 / 180.0; 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; } 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; } /** * 地球坐标转换为火星坐标 * World Geodetic System ==> Mars Geodetic System * * @param wgLat 地球坐标 * @param wgLon * * mglat,mglon 火星坐标 */ void transform2Mars(double wgLat, double wgLon,double &mgLat,double &mgLon) { if (outOfChina(wgLat, wgLon)) { mgLat = wgLat; mgLon = wgLon; return ; } double dLat = transformLat(wgLon - 105.0, wgLat - 35.0); double dLon = transformLon(wgLon - 105.0, wgLat - 35.0); double radLat = wgLat / 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); mgLat = wgLat + dLat; mgLon = wgLon + dLon; } /** * 火星坐标转换为百度坐标 * @param gg_lat * @param gg_lon */ void bd_encrypt(double gg_lat, double gg_lon,double &bd_lat,double & bd_lon) { double x = gg_lon, y = gg_lat; double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi); double theta = atan2(y, x) + 0.000003 * cos(x * x_pi); bd_lon = z * cos(theta) + 0.0065; bd_lat = z * sin(theta) + 0.006; } /** * 百度转火星 * @param bd_lat * @param bd_lon */ void bd_decrypt(double bd_lat, double bd_lon,double &gg_lat,double &gg_lon) { double x = bd_lon - 0.0065, y = bd_lat - 0.006; double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi); double theta = atan2(y, x) - 0.000003 * cos(x * x_pi); gg_lon = z * cos(theta); gg_lat = z * sin(theta); } int main() { double lat = 30.227607; double lon = 120.036565; //真实的经纬度转化为百度地图上的经纬度,便于计算百度POI double marsLat = 0; double marsLon = 0; double resultLat = 0; double resultLon = 0; transform2Mars(lat,lon,marsLat,marsLon); bd_encrypt(marsLat,marsLon,resultLat,resultLon); //30.2193456 120.0348264 cout<<setprecision(10)<<resultLat<<" "<<setprecision(10)<<resultLon<<endl; }在自己的项目中,将标准GPS转换为百度坐标后,再获取百度地图的POI,获得的POI位置和真实的位置基本一致,可以使用。下面还有一些网上转载的资料,比较丰富,值得一看。
-----------------------------------------------------------------------------------------------------
查询过资料可得,通过程序进行 标准坐标转火星坐标(google、高德),火星坐标转百度坐标,百度坐标转火星坐标,百度坐标转标准坐标都是可行的,火星直接转标准坐标转不了。