编写读写GPS数据程序,熟悉GPS格式
1 熟悉GPS格式
对GPS中的各种格式所代表的内容进行熟悉,见附件EXCEL文件。
2 编写代码
对某一格式的GPS数据(数据文件:gps.txt)中的X,Y坐标读出,并通过界面绘出。
代码编写可采用任何一种语言,推荐VB(实验室电脑有VB C++等)
注意以下问题:
(1)编写读取文件功能;
(2)数组使用;
(3)界面编写存在比例问题。
(4)对于电脑屏幕的0,0坐标倒置可以忽略。
测试代码(C++版 - 验证中…)
// 练习简易画图View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "练习简易画图.h"
#include "练习简易画图Doc.h"
#include "练习简易画图View.h"
#include <vector>
#include <algorithm>
#include <ostream>
#include <fstream>
#include<iostream>
#include<iomanip>
#include<string>
#include<sstream>
using namespace std;
//数据类型转换模板函数
template <class Type>
Type stringToNum(const string str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
ON_COMMAND(ID_LINE, OnLine)
ON_COMMAND(gps, Ongps)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
shape=1;
w=1;
penstyle=PS_SOLID;
m_down=false;
m_ncolor=RGB(0,0,0);
m_fcolor=RGB(0,0,0);
// TODO: add construction code here
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// m_down=true;
// SetCapture();
// point1=point;
// ReleaseCapture();
// // TODO: Add your message handler code here and/or call default
// CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
// if(m_down)
// {
// point2=point;
// CClientDC dc(this);
// }
// TODO: Add your message handler code here and/or call default
// CView::OnMouseMove(nFlags, point);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_down=false;
Invalidate(0);
// TODO: Add your message handler code here and/or call default
CView::OnLButtonUp(nFlags, point);
}
void CMyView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// CPaintDC dc(this); // device context for painting
CPen pen;
CBrush brush;
pen.CreatePen(penstyle,w,m_ncolor);
brush.CreateSolidBrush(m_fcolor);
dc.SelectObject(&pen);
dc.SelectObject(&brush);
//dc.TextOut(100,100,m_show);
if(1==shape)
{
dc.MoveTo(point1.x,point1.y);
dc.LineTo(point2.x,point2.y);
//dc.MoveTo(10,20);
//dc.LineTo(30,40);
}
// TODO: Add your message handler code here
// Do not call CView::OnPaint() for painting messages
}
void CMyView::OnLine()
{
shape=1;
}
void CMyView::Ongps()
{
// TODO: Add your command handler code here
ifstream is("gps.txt");
std::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);
is.close();
// int length = _ttoi(_T(arr1[2]));//字符串转整型
// MessageBox(arr1[2]);
// cout<<"x"<<endl;
//构造日期时间字符串 YYYY-MM-DD HH:MM:SS
// float d0 = stringToNum<float>(arr1[2]);
// float d1 = stringToNum<float>(arr1[4]);
// cout<<"经度:";
// printf("%lf\n",d0/100);
// shape=1;
// point1.x=d0/100;
// point1.y=d1/100;
// point2.x=d0/100+10;
// point2.y=d1/100+10;
//此处不能使用cout,否则输出精度将受到影响
// cout<<"纬度:";
// printf("%lf\n",d1/100);
// cout<<"海拔:"<<arr1[9]<<arr1[10]<<endl;
}
程序运行界面
完整项目源码获取
有需要项目源代码的小伙伴
可以在海轰的微信公众号:海轰Pro
回复:海轰
就可以啦(注意看使用说明哦o( ̄︶ ̄)o)