C++~解析GPS数据示例

一、GPS数据解析
https://blog.csdn.net/qq_31905135/article/details/81114114

二、C++ 解析GPS数据

C++ 解析GPS数据示例:  
GPS数据类型示例:  
$GPGGA,025620.00,2602.33721,N,11911.49176,E,2,04,1.63,13.5,M,9.9,M,,0000*5D 
$GPRMC,025620.20,A,2602.33722,N,11911.49176,E,0.100,,281211,,,D*79  
$GPGGA,025620.20,2602.33722,N,11911.49176,E,2,04,1.63,13.3,M,9.9,M,,0000*5A 
$GPRMC,025620.40,A,2602.33723,N,11911.49174,E,0.098,,281211,,,D*7C   
 
//示例程序  VS 测试通过  
 
/*  功能 C++解析GPS数据并显示  时间 2016-04-22*/  
#include 
#include 
#include 
#include 
#include 
using namespace std; 
 
//数据类型转换模板函数 
template <class Type>    
Type stringToNum(const string str)   
{        
	istringstream iss(str);       
	Type num;       
	iss >> num;       
	return num;       
}  
 
int main() 
{  
	ifstream is("D:/gpstest.txt");  
	string line;  
	getline(is,line);    
	//读取第一行 GGA格式  $GPGGA,025620.00,2602.33721,N,11911.49176,E,2,04,1.63,13.5,M,9.9,M,,0000*5D
	
	//GGA字段含义   语句ID,UTC时间,纬度,N/S,经度,E/W,GPS状态,卫星数量,精度因子,海拔 。。。   
	vector<string> arr1;  //定义一个字符串容器  
	int position = 0;  
	do 
	{   
		string tmp_s;    position = line.find(","); //找到逗号的位置   
		tmp_s = line.substr(0,position); //截取需要的字符串    
		line.erase(0,position+1); //将已读取的数据删去     
		arr1.push_back(tmp_s);   //将字符串压入容器中  
	}while(position != -1); 
	
	position = 0;  
	getline(is,line);   
	
	//读取第二行 RMC格式  $GPRMC,025620.20,A,2602.33722,N,11911.49176,E,0.100,,281211,,,D*79  
	//RMC字段含义  语句ID,UTC时间,GPS状态,纬度,N/S,经度,E/W,速度(节),,UTC日期,,,校验值   
	vector<string> arr2;   
	do 
	{   
		string tmp_s;    
		position = line.find(",");   
		tmp_s = line.substr(0,position);   
		line.erase(0,position+1);    
		arr2.push_back(tmp_s);   
	}while(position != -1);  
	is.close();   
	//构造日期时间字符串 YYYY-MM-DD HH:MM:SS   
	string datetime = "20"+arr2[9].substr(4,2)+"-"+arr2[9].substr(2,2)+"-"+arr2[9].substr(0,2)+" "+arr1[1].substr(0,2)+
		":"+arr1[1].substr(2,2)+":"+arr1[1].substr(4,2);   
	cout<<"UTC时间:"<<datetime<<endl;
	double d0 = stringToNum<double>(arr1[2]);  
	cout<<"经度:";  
	printf("%lf\n",d0/100);   
	//此处不能使用cout,否则输出精度将受到影响  
	double d1 = stringToNum<double>(arr1[4]);  
	cout<<"纬度:";  
	printf("%lf\n",d1/100);   
	cout<<"海拔:"<<arr1[9]<<arr1[10]<<endl;  
	return 0; 
}    
 
/*     运行结果      
UTC时间:2011-12-28 02:56:20     
经度:26.023372     
纬度:119.114918     
海拔:13.5M        
*/

你可能感兴趣的:(C++~解析GPS数据示例)