地球坐标,火星坐标,百度坐标转换
标准地球坐标即GPS设备获得的坐标,该坐标需要经过国家测绘局进行加密后形成火星坐标(GCJ-02 ),我们用的google坐标和高德地图坐标也就是火星坐标
百度地图,在火星坐标的基础上再进行一次加密,形成了百度地图上的坐标,因此,直接将标准地球坐标显示在百度地图上是会有几百米的偏差的。按照此原理,标准GPS坐标经过两步的转换可得到百度坐标。因为在处理百度地图时,例如查询其POI都需要百度地图上的坐标,而不是标准坐标,那么这样的转换就是必要的了。下面是两步转换的C++ 程序,供开发的人做一个参考。
标准gps WGS-84 :gps硬件 ,google地球
火星坐标 GCJ-02:google地图 、 高德地图
百度坐标BD-09 :百度坐标转换
注意: 一般从GPS设备上取到的是标准GPS数据,也就是原始第一层数据, 同时其可能是度、度分、度分秒的格式数据,最后都要转化为度的数据进行运算,转化方式如下
度格式 = 度+分/60;(数据为度分)
例子 30°33.1786(度分格式) = (度格式) 30 +33.1786/60= 30.552976666666666666666666666667(度格式);
度格式 =度+分/60 +秒/3600;(数据为度分秒)
同上
//============================================================================ // 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; } 下面是 c#代码 public class GisHelper { private const double EARTH_RADIUS = 6378137; /// <summary> /// 计算两点位置的距离,返回两点的距离,单位:米 /// 该公式为GOOGLE提供,误差小于0.2米 /// </summary> /// <param name="lng1">第一点经度</param> /// <param name="lat1">第一点纬度</param> /// <param name="lng2">第二点经度</param> /// <param name="lat2">第二点纬度</param> /// <returns></returns> public static double GetDistance(double lng1, double lat1, double lng2, double lat2) { double radLat1 = Rad(lat1); double radLng1 = Rad(lng1); double radLat2 = Rad(lat2); double radLng2 = Rad(lng2); double a = radLat1 - radLat2; double b = radLng1 - radLng2; double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS; return result; } /// <summary> /// 经纬度转化成弧度 /// </summary> /// <param name="d"></param> /// <returns></returns> private static double Rad(double d) { return (double)d * Math.PI / 180d; } const double pi = 3.14159265358979324; // // Krasovsky 1940 // // a = 6378245.0, 1/f = 298.3 // b = a * (1 - f) // ee = (a^2 - b^2) / a^2; const double a = 6378245.0; const double ee = 0.00669342162296594323; /// <summary> /// 经纬度纠偏 /// </summary> /// <param name="wgLat">原始纬度</param> /// <param name="wgLon">原始经度</param> /// <param name="mgLat">纠偏后纬度</param> /// <param name="mgLon">就偏厚经度</param> public static void Transform(double wgLat, double wgLon, out double mgLat, out 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 = Math.Sin(radLat); magic = 1 - ee * magic * magic; double sqrtMagic = Math.Sqrt(magic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi); mgLat = wgLat + dLat; mgLon = wgLon + dLon; } static 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; } static 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 * Math.Sqrt(Math.Abs(x)); ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0; ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0; return ret; } static double TransformLon(double x, double y) { double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x)); ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0; ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0; return ret; } } 下面是 javascpept var a = 6378245.0; var pi=3.14159265358979324; var ee = 0.00669342162296594323; var x_pi = 3.14159265358979324 * 3000.0 / 180.0; var mgLat=0.0000; var mgLon=0.0000; var bd_lat=0.0000; var bd_lon=0.0000; var lastpoint=[0,0]; var sample_size = 0; var pointsCount = 0; var outOfChina=function( lat, lon){ if (lon < 72.004 || lon > 137.8347) return true; if (lat < 0.8293 || lat > 55.8271) return true; return false; } var transformLat= function( x, y){ var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); ret =ret+ (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0; ret =ret+ (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0; ret =ret+ (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0; return ret ; } var transformLon=function( x, y){ var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0; return ret; } var transform2Mars=function( wgLat, wgLon){ if (outOfChina(wgLat, wgLon)) { mgLat = wgLat; mgLon = wgLon; return ; } var dLat = transformLat(wgLon - 105.0, wgLat - 35.0); var dLon = transformLon(wgLon - 105.0, wgLat - 35.0); var radLat = wgLat / 180.0 * pi; var magic = Math.sin(radLat); magic = 1 - ee * magic * magic; var sqrtMagic = Math.sqrt(magic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi); mgLat = wgLat + dLat; mgLon = wgLon + dLon; } var bd_encrypt=function( gg_lat, gg_lon) { var x = gg_lon, y = gg_lat; var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); var theta =Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); bd_lon = z * Math.cos(theta) + 0.0065; bd_lat = z * Math.sin(theta) + 0.006; }
在自己的项目中,将标准GPS转换为百度坐标后,再获取百度地图的POI,获得的POI位置和真实的位置基本一致,可以使用。下面还有一些网上转载的资料,比较丰富,值得一看。
- GCJ-02坐标系统(火星坐标)简介:http://blog.csdn.net/giswens/article/details/8775121(存档:http://mapbd.com/cms/2012/07/25/)
- GCJ-02到真实坐标反向变换的理论基础:http://blog.csdn.net/giswens/article/details/8775213
- WGS84坐标与Web墨卡托坐标互转:http://blog.csdn.net/giswens/article/details/9634261
- 地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法:
- C#代码:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936(注解:http://blog.csdn.net/giswens/article/details/8775283)
- Java代码:http://emq.googlecode.com/svn/emq/src/Algorithm/Coords/Converter.java
- iOS代码:http://blog.csdn.net/giswens/article/details/8775183(存档:http://www.keakon.net/2011/07/02/WGS84坐标转火星坐标(iOS篇))
- 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法:http://blog.csdn.net/coolypf/article/details/8569813(解释:国际经纬度坐标标准为WGS-84,国内必须至少使用国测局制定的GCJ-02,对地理位置进行首次加密。百度坐标在此基础上,进行了BD-09二次加密措施,更加保护了个人隐私。百度对外接口的坐标系并不是GPS采集的真实经纬度,需要通过坐标转换接口进行转换。)
- 一种根据纠偏数据对火星坐标进行完美拟合的方法:http://blog.sina.com.cn/s/blog_538036cf0100pxbl.html
- 国内各地图API坐标系统比较:http://rovertang.com/archives/547
- 关于百度地图坐标转换接口的研究:http://rovertang.com/archives/24699
查询过资料可得,通过程序进行 标准坐标转火星坐标(google、高德),火星坐标转百度坐标,百度坐标转火星坐标,百度坐标转标准坐标都是可行的,火星直接转标准坐标转不了。
文章来源: https://blog.csdn.net/qq_36051316/article/details/86534379