⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maptestdoc.cpp

📁 gps 可以打开.gps
💻 CPP
字号:
// MapTestDoc.cpp : implementation of the CMapTestDoc class
//

#include "stdafx.h"
#include "MapTest.h"
#include "MapTestView.h"
#include "MapTestDoc.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW

#define ABS( a )	( ((a) >= 0 ) ? (a) : -(a) )   //定义一个绝对值


#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMapTestDoc

IMPLEMENT_DYNCREATE(CMapTestDoc, CDocument)

BEGIN_MESSAGE_MAP(CMapTestDoc, CDocument)
	//{{AFX_MSG_MAP(CMapTestDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapTestDoc construction/destruction

CMapTestDoc::CMapTestDoc()
{
	// TODO: add one-time construction code here
	// TODO: add one-time construction code here
	m_pMemDC = NULL;
	m_pBitmap = NULL;
	m_pData = NULL;
	m_nMaxPoint = 0;
	//设置初始偏移位置
	m_nOffsetX = 0;
	m_nOffsetY = 0;
	ONum=0;
    mif_gps=true;
	//初始化文档标题
//	m_strMapName = _T("无地图");

	////////////ldq添加////////////

	m_fMinX=180;
	m_fMaxX=-180;
	m_fMinY=90;
	m_fMaxY=-90;


	m_fMapjw[0]=112.0;//地图左边界经度
	m_fMapjw[1]=127.0;//地图右边界经度
	m_fMapjw[2]=35.0;//地图上边界纬度
	m_fMapjw[3]=22.0;//地图下边界纬度(南纬为负)
	m_fMaxCX=526.875000;  //当前地图最大显示范围(直角坐标)
	m_fMaxCY=660.000000;  //
//	m_fMaxCX=423;
 //   m_fMaxCY=753;
}

CMapTestDoc::~CMapTestDoc()
{
   	if(m_aLayers.GetSize()>0)
	{
		int i;
		for(i = m_aLayers.GetSize(); i>0; i--)
		{
			delete m_aLayers.GetAt(i-1);
		}
		m_aLayers.RemoveAll();
	}

	if(m_pMemDC)
	{//清除程序创建的DC资源
		m_pMemDC->SelectObject(m_pDefaultBitmap);
		m_pMemDC->DeleteDC();
		m_pMemDC = NULL;
	}

	if(m_pBitmap)
	{//清除程序创建的BITMAP资源
		m_pBitmap->DeleteObject();
		delete m_pBitmap;
		m_pBitmap = NULL;
	}

	if(m_pData)
	{//清除绘图用整数数组
		m_nMaxPoint = 0;
		delete[] m_pData;
		m_pData = NULL;
	}
}

BOOL CMapTestDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMapTestDoc serialization

void CMapTestDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMapTestDoc diagnostics

#ifdef _DEBUG
void CMapTestDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMapTestDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMapTestDoc commands

//转换为逻辑坐标
void CMapTestDoc::DPtoLP(long &x, long &y)
{
	x -= m_nOffsetX;
	y -= m_nOffsetY;
}

//经纬度转换直角坐标
void CMapTestDoc::ConvToXY(double jing, double wei, long &cx, long &cy)
{
	cx = (long)((m_fMaxCX * (jing-m_fMapjw[0]))/(m_fMapjw[1]-m_fMapjw[0])+0.5);
	cy = (long)((m_fMaxCY * (relfa(m_fMapjw[2])-relfa(wei)))/(relfa(m_fMapjw[2]) - relfa(m_fMapjw[3]))+0.5);
}
//经纬度(度)->直角坐标多点转换
void CMapTestDoc::ConvToXYs(double *jw, long *xy, int n)  
{
	int i;
	double jing, wei;
	for(i=0;i<n;i++)
	{
		jing = *(jw + i*2);
		wei = *(jw + i*2 + 1);
		*(xy + i*2) = (long)((m_fMaxCX * (jing-m_fMapjw[0]))/(m_fMapjw[1]-m_fMapjw[0])+0.5);
		*(xy + i*2+1) = (long)((m_fMaxCY * (relfa(m_fMapjw[2])-relfa(wei)))/(relfa(m_fMapjw[2]) - relfa(m_fMapjw[3]))+0.5);
	}
}
//直角坐标转换为经纬度(度)
void CMapTestDoc::ConvToJW(long cx, long cy, double &jing, double &wei) 
{
	jing = m_fMapjw[0] + cx*(m_fMapjw[1] - m_fMapjw[0]) / m_fMaxCX;
	wei = (absfa(relfa(m_fMapjw[2]) - cy * (relfa(m_fMapjw[2]) - relfa(m_fMapjw[3])) / m_fMaxCY));
	for(; jing > 180.0 ; jing -=360);
	for(; jing < -180.0 ; jing += 360);
}


void CMapTestDoc::Zoom(float scale)
{
 	m_fMaxCX *= scale;
	m_fMaxCY *= scale;
}

//设置地图显示的中心
void CMapTestDoc::SetCenter(CPoint point)
{
	m_nOffsetX = m_nViewWidth / 2 - point.x;
	m_nOffsetY = m_nVeiwHeight / 2 - point.y;
}

///绘制地图
void CMapTestDoc::DrawMap(CDC *pDC)
{

	int i;
	int j = m_aLayers.GetSize(); //获得地图数组中的图层数,即mif文件数;
	CMapLayer* pLayer;

	//清空原位图
	pDC->BitBlt(0, 0, m_nViewWidth, m_nVeiwHeight, NULL, 0, 0, WHITENESS);

	//以下程序开始绘制地图
	//由于地图中各图元重叠时会互相遮挡,必须分别绘制
	//绘制顺序为:面图元,线图元,点图元(文字等)

	for(i=0; i<j; i++)
	{//遍历各地图层,绘区域图元
		pLayer = m_aLayers.GetAt(i);
		if(pLayer->m_bCanDraw)
		{
			pLayer->Draw(pDC, IDS_POLYGON);
		}
	}

	for(i=0; i<j; i++)
	{//遍历各地图层,绘折线图元
		pLayer = m_aLayers.GetAt(i);
		if(pLayer->m_bCanDraw)
		{
			pLayer->Draw(pDC, IDS_PLINE);
		}
	}

	for(i=0; i<j; i++)
	{//遍历各地图层,绘点图元

		pLayer = m_aLayers.GetAt(i);  //定义在图层元素中当前的图层
		if(pLayer->m_bCanDraw)
		{
			pLayer->Draw(pDC, IDS_POINT);
		}
	}

	for(i=0; i<j; i++)
	{//遍历各地图层,绘文字图元
		pLayer = m_aLayers.GetAt(i);
		if(pLayer->m_bCanDraw)
		{
			pLayer->Draw(pDC, IDS_TEXT);
		}
	}
}


void CMapTestDoc::LPtoDPs(long *data, int n) //将逻辑坐标转换为显示坐标
{
	int i;

	for(i=0; i<n; i++)
	{
		*(data + 2*i) += m_nOffsetX;
		*(data + 2*i+1) += m_nOffsetY;
	}

}

void CMapTestDoc::LPtoDP(long &x, long &y) //地图逻辑坐标转换为屏幕坐标
{
	x += m_nOffsetX;
	y += m_nOffsetY;
}

void CMapTestDoc::MoveOffset(CPoint point) //重新赋值屏幕的偏移量
{
	m_nOffsetX += point.x;
	m_nOffsetY += point.y;
}

void CMapTestDoc::DisplayJW(CPoint point)
{
	if(!m_aLayers.GetSize())
	{//未打开海图
		return;
	}

	CStatusBar* pStatus = (CStatusBar*) AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
		//AFX_IDW_STATUS_BAR 为系统预定义常量(StatusBar的缺省ID)

	CPoint pt = point;  //定义一个点,
	double jing, wei;

	DPtoLP(pt.x, pt.y);//转换为逻辑坐标
	ConvToJW(pt.x, pt.y, jing, wei);//转换为经纬度
	CString strlon, strlat;
	int degree = jing;
	int minute = (jing - degree)*60;
	int secend = (jing - degree)*3600 - minute*60;
	if(jing<0)
		strlon.Format("西经:%3d度 %2d分 %2d秒 ", -degree, -minute, -secend);
	else
		strlon.Format("东经:%3d度 %2d分 %2d秒", degree, minute, secend);

	degree = wei;
	minute = (wei - degree)*60;
	secend = (wei - degree)*3600 - minute*60;
	if(wei<0)
		strlat.Format("南纬:%3d度 %2d分 %2d秒  %d,%d  ", -degree, -minute, -secend,pt.x,pt.y);
	else
		strlat.Format("北纬:%3d度 %2d分 %2d秒  %d,%d  ", degree, minute, secend,pt.x,pt.y);

	pStatus->SetPaneText(0, strlon+strlat);//在状态条中显示经纬度

}
	

double CMapTestDoc::GetDistance(double sx, double sy, double dx, double dy)//计算两点间距离 直线距离
{
	
     double  dd=sqrt(ABS((dx-sx)*(dx-sx))     
						+ABS((dy-sy)*(dy-sy))); 
	   return dd;
}

void CMapTestDoc::InitData()   //清理用户数据
{
	if(m_aLayers.GetSize()>0)
	{
		int i;
		for(i = m_aLayers.GetSize(); i>0; i--)
		{
			delete m_aLayers.GetAt(i-1);
		}
		m_aLayers.RemoveAll();
	}
	
	if(m_nMaxPoint)
	{//清除绘图用整数数组
		m_nMaxPoint = 0;
		delete[] m_pData;
		m_pData = NULL;		
	}
	
	//设置初始偏移位置
	m_nOffsetX = 0;
	m_nOffsetY = 0;
	
	//初始化文档标题
}

void CMapTestDoc::SetMapReach(double dx,double dy) //设置地图显示区域的大小,dx dy为地图的实际大小
{
	if( (38*dx) < m_nViewWidth && 38*dy < m_nVeiwHeight )
	{
		if(m_nViewWidth /38*dx < m_nVeiwHeight/38*dy)
		{
			m_fMaxCX=m_nViewWidth-50 ;  //当前地图最大显示范围(直角坐标)
			m_fMaxCY=dy*(m_nViewWidth /dx);  //
		}
		else
		{
			//m_fMaxCX=dx*(m_nVeiwHeight/dy-);  //当前地图最大显示范围(直角坐标)
			m_fMaxCY=m_nVeiwHeight;  //
		}
	}
	else if( (38*dx) > m_nViewWidth || (38*dy) > m_nVeiwHeight )
	{
		if((38*dx) > m_nViewWidth)
		{
			m_fMaxCX=m_nViewWidth;  //当前地图最大显示范围(直角坐标)
			m_fMaxCY=dy*(m_nViewWidth/dx);  //
		}
		else if(38*dy > m_nVeiwHeight)
		{
			m_fMaxCX=dx*(m_nVeiwHeight/dy);  //当前地图最大显示范围(直角坐标)
			m_fMaxCY=m_nVeiwHeight;  //
			
		}		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -