gps数据转坐标c语言,GPS原始坐标转百度地图坐标(纯C代码)
一、環境介紹
GPS模塊型號:中科微電子GPS模塊
GPS輸出的原始數據幀:
$GNGGA,114955.000,2842.4158,N,11549.5439,E,1,05,3.8,54.8,M,0.0,M,,*4F
$GNGLL,2842.4158,N,11549.5439,E,114955.000,A,A*4D
$GPGSA,A,3,10,31,18,,,,,,,,,,5.7,3.8,4.2*37
$BDGSA,A,3,07,10,,,,,,,,,,,5.7,3.8,4.2*2A
$GPGSV,3,1,10,10,49,184,42,12,16,039,,14,54,341,,18,22,165,23*7B
$GPGSV,3,2,10,22,11,318,,25,51,055,,26,24,205,,29,13,110,*7C
$GPGSV,3,3,10,31,50,287,36,32,66,018,*7F
$BDGSV,1,1,04,03,,,07,05,,,29,07,79,246,33,10,52,232,19*62
$GNRMC,114955.000,A,2842.4158,N,11549.5439,E,0.00,44.25,061117,,,A*4D
$GNVTG,44.25,T,,M,0.00,N,0.00,K,A*14
$GNZDA,114955.000,06,11,2017,00,00*47
$GPTXT,01,01,01,ANTENNA OK*35
二、需求介紹
得到GPS原始坐標數據之后,想通過百度地圖API接口直接顯示實際定位。
國際經緯度坐標標準為WGS-84,國內必須至少使用國測局制定的GCJ- 02,對地理位置進行首次加密。
百度坐標在此基礎上,進行了BD-09二次加密措施,更加保護了個人隱私。
百度對外接口的坐標系并不是GPS采集的真實經 緯度,需要通過坐標轉換接口進行轉換。
三、C語言代碼
下面代碼在QtCreator里編寫,可以將代碼移植到任何支持C語言的環境中編譯運行。
#include #include "QString"
#include extern "C"
{
#include "math.h"
}
class GPS_Data
{
public:
double lat; //緯度
double lng; //經度
QString GPS_Data;
};
class GPS_Data gps_data;
void GPS_ReadUasrtData();
#define M_PI 3.14159265358979324
double a = 6378245.0;
double ee = 0.00669342162296594323;
double x_pi = M_PI * 3000.0 / 180.0;
double wgs2gcj_lat(double x, double y)
{
double ret1 = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
ret1 += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret1 += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
ret1 += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
return ret1;
}
double wgs2gcj_lng(double x, double y)
{
double ret2 = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
ret2 += (20.0 *sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret2 += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
ret2 += (150.0 *sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
return ret2;
}
void wgs2gcj(double *lat,double *lng)
{
double dLat = wgs2gcj_lat(*lng - 105.0, *lat - 35.0);
double dLon = wgs2gcj_lng(*lng - 105.0, *lat - 35.0);
double radLat = *lat / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
*lat = *lat + dLat;
*lng = *lng + dLon;
}
void gcj2bd(double *lat,double *lng)
{
double x = *lng, y = *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);
*lng = z * cos(theta) + 0.0065;
*lat = z * sin(theta) + 0.006;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
GPS_ReadUasrtData();
//得到GPS原始坐標
double lat=gps_data.lat;
double lng=gps_data.lng;
//坐標轉換
wgs2gcj(&lat,&lng);
gcj2bd(&lat,&lng);
//得到百度地圖的坐標,可以直接在百度地圖上顯示
qDebug()<200)
{
int index=gps_data.GPS_Data.indexOf("$GNGGA");
if(index>=0)
{
QString text=gps_data.GPS_Data.mid(index);
if(text.size()>60)
{
QString lat=text.section(',',2,2);
QString lng=text.section(',',4,4);
if(lat.isEmpty()==false && lng.isEmpty()==false)
{
unsigned int int_data;
double s_Longitude,s_latitude;
//轉換緯度
s_latitude=lat.toDouble();
s_latitude=s_latitude/100;
int_data=s_latitude;//得到緯度整數部分
s_latitude=s_latitude-int_data;//得到緯度小數部分
s_latitude=(s_latitude)*100;
gps_data.lat=int_data+(s_latitude/60.0); //得到轉換后的值
//轉換經度
s_Longitude=lng.toDouble();
s_Longitude=s_Longitude/100;
int_data=s_Longitude;//得到經度整數部分
s_Longitude=s_Longitude-int_data; //得到經度小數部分
s_Longitude=s_Longitude*100;
//gai guo le
gps_data.lng=int_data+(s_Longitude/60.0);
}
}
}
gps_data.GPS_Data.clear();
}
}
下面公眾號有全套的C語言、單片機、QT、C++、Python的基礎教程,歡迎關注:
總結
以上是生活随笔為你收集整理的gps数据转坐标c语言,GPS原始坐标转百度地图坐标(纯C代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玩游戏不再卡顿!网游CPU内存让你畅享流
- 下一篇: c语言 编程显示图案*,*型图案的显示与