一. 数据结构
- /////////////////////////////////////////////////////数据结构////////////////////////////////
- //导弹和飞机的位置
- typedef struct _Position
- {
- double m_longtidude; //经度
- double m_latitude; //纬度
- bool flag; //标识位
- } Position;
- typedef std::vector<Position> _planePositionVector; //存储飞机的轨迹
- typedef std::vector<Position> _missilePositionVector; //存储导弹的轨迹
- ///////////////////////////////////////////////////////////////////////////////////////////
二. 接口
- //////////////////////////////////陕西测绘中心地图接口///////////////////
- public:
- int LoadGeoData(CString file); //加载地图
- void CreateLayer(CMapXLayer* layer, CString str, int id ); //创建图层
- void DrawPoint(CMapXLayer* layer,Position position, CString bitName); //在地图上画点
- void DrawLine(CMapXLayer* layer, Position startPoint, Position endPoint, int lineColor, int lineStyle);//画轨迹线
- void ShowPlaneTrace(_planePositionVector &planeVector); //展示飞机的轨迹
- void ShowMissileTrace(_missilePositionVector &missileVector); //展示导弹的轨迹
- void RecordPlaneTrace(_planePositionVector &planeVector, Position &positionStart, int count); //记录飞机的轨迹
- void RecordMissileTrace(_missilePositionVector &missileVector, Position &positionStart, int count); //记录导弹的轨迹
- void PlaneNextPosition(Position &startPosition, Position &endPosition); //改变飞机的行动轨迹
- void MissileNextPosition(Position &startPosition, Position &endPosition); //改变导弹的行动轨迹
- public:
- CMapXLayer m_planeLayer; //飞机图层
- CMapXLayer m_missileLayer; //导弹图层
- CMapXLayer m_trackLayer; //飞行轨迹图层
- CMapX m_map; //地图对象
三. 接口实现
- //加载地图
- int CLmtestView::LoadGeoData(CString file)
- {
- if (!m_map.Create(NULL,WS_VISIBLE,CRect(50,50,400,400),this, IDC_MAP))
- {
- //创建失败
- return -1;
- }
- m_map.SetGeoSet(file);
- m_map.SetTitleText("导弹攻击沙盘演示V1.0");
- //CreateDefenseLayer(); //创建图层
- CreateLayer(&m_planeLayer,"PLANE_LAYER",2); //创建飞机图层
- CreateLayer(&m_missileLayer,"MISSILE_LAYER",3); //创建导弹图层
- CreateLayer(&m_trackLayer,"TRACK_LAYER",4); //创建飞行轨迹图层
- m_map.SetCurrentTool(miPanTool);
- return 0;
- }
- //创建图层
- void CLmtestView::CreateLayer(CMapXLayer* layer, CString str, int id )
- {
- CString geo=m_map.GetGeoSet();
- if(geo.IsEmpty())
- {
- AfxMessageBox(_T("未指定地图集,请先指定地图集"));
- return;
- }
- try
- {
- m_map.GetLayers().CreateLayer(_T(str), NULL, id);
- *layer = m_map.GetLayers().Item(_T(str));
- }
- catch(COleDispatchException *e)
- {
- e->ReportError();
- e->Delete();
- }
- catch(COleException *e)
- {
- e->ReportError();
- e->Delete();
- }
- }
- //创建导弹图层
- void CLmtestView::OnPlane()
- {
- // TODO: Add your command handler code here
- //设置飞机飞行定时器
- SetTimer(ID_TIMER_PLANE,1000,NULL);
- }
- void CLmtestView::OnMissile()
- {
- // TODO: Add your command handler code here
- SetTimer(ID_TIMER_MISSILE,1000,NULL);
- }
- void CLmtestView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- //触发定时器
- if (ID_TIMER_PLANE == nIDEvent)
- {
- static bool binit = false;
- if (!binit) //该语句可以保证只设置一次
- {
- //设置飞机的起始点
- Position planePositionStart ={87.1875,43.961191,FALSE};
- //设置飞机的飞行轨迹
- RecordPlaneTrace(planePositionVector,planePositionStart,10);
- binit = true;
- }
- //展示飞机的飞行轨迹
- ShowPlaneTrace(planePositionVector);
- }
- else if(ID_TIMER_MISSILE == nIDEvent)
- {
- static bool binit = false;
- if (!binit) //该语句可以保证只设置一次
- {
- //设置导弹的起始点
- Position missilePositionStart ={128.71582,46.498392,FALSE};
- //设置导弹的飞行轨迹
- RecordMissileTrace(missilePositionVector,missilePositionStart,10);
- binit = true;
- }
- //展示飞机的飞行轨迹
- ShowMissileTrace(missilePositionVector);
- }
- CView::OnTimer(nIDEvent);
- }
- //画线
- void CLmtestView::DrawLine(CMapXLayer* layer, Position startPoint, Position endPoint, int lineColor, int lineStyle)
- {
- CMapXStyle lineSty; //线图元
- COleVariant vt;
- vt.vt=VT_DISPATCH;
- CMapXFeature newFeature; //新建图元
- CMapXPoint pt; //点图元
- CMapXPoints pts; //点集合
- pt.CreateDispatch(pt.GetClsid());
- pts.CreateDispatch(pts.GetClsid());
- pts.RemoveAll();
- pt.Set(startPoint.m_longtidude, startPoint.m_latitude); //线的起点
- pts.Add(pt);
- pt.Set(endPoint.m_longtidude,endPoint.m_latitude); //线的终点
- pts.Add(pt);
- vt.pdispVal=pts.m_lpDispatch;
- vt.pdispVal->AddRef();//此句不可缺
- CMapXFeature sidefeature=m_map.GetFeatureFactory().CreateLine(vt); //创建线图元
- lineSty=sidefeature.GetStyle(); //设置线图元的风格
- lineSty.SetLineColor(lineColor);
- lineSty.SetLineStyle(lineStyle);
- sidefeature.SetStyle(lineSty.m_lpDispatch);
- sidefeature=(*layer).AddFeature(sidefeature); //将创建的图元加入到图层中
- sidefeature.Update();
- // long sid=sidefeature.GetFeatureID();//过去图元的ID号,此ID号只能读,不能写
- // m_sidearray.Add(sid);
- m_map.Refresh();
- }
- void CLmtestView::DrawPoint(CMapXLayer* layer,Position position, CString bitName) //在地图上画点
- {
- //在指定的位置处增加导弹图标
- CMapXFeature ftr;
- if(!ftr.CreateDispatch (ftr.GetClsid ()))
- {
- TRACE(_T("SetMissilePositon FAILED\n"));
- }
- try
- {
- ftr.Attach (m_map.GetDispatch ());
- //设置风格
- CMapXStyle style=ftr.GetStyle ();
- style.SetSymbolType(miSymbolTypeBitmap);
- style.SetSymbolBitmapSize (24);
- style.SetSymbolBitmapTransparent (TRUE);
- style.SetSymbolBitmapName(_T(bitName));
- ftr.GetPoint ().Set(position.m_longtidude, position.m_latitude); //设置经纬度
- ftr=(*layer).AddFeature(ftr);//此处语法奇怪 必须返回值 无资料可查
- m_map.Refresh ();
- }
- catch(COleDispatchException *e)
- {
- e->ReportError();
- e->Delete();
- }
- catch(COleException *e)
- {
- e->ReportError();
- e->Delete();
- }
- }
- //改变飞机的行动轨迹
- void CLmtestView::PlaneNextPosition(Position &startPosition, Position &endPosition)
- {
- static int flag = 1; //转弯标识
- if (flag<3)
- {
- static double longtidudeStep = 2.03;
- static double latitudeStep = 2.26;
- endPosition.flag = false;
- endPosition.m_longtidude = startPosition.m_longtidude + longtidudeStep; //经度步长+5
- endPosition.m_latitude = startPosition.m_latitude - latitudeStep; //纬度步长-6
- longtidudeStep += 0.89;
- latitudeStep += 0.35;
- flag++;
- }
- else if(flag <4)
- {
- static double longtidudeStep = 2.03;
- static double latitudeStep = 4.26;
- endPosition.flag = false;
- endPosition.m_longtidude = startPosition.m_longtidude + longtidudeStep; //经度步长+5
- endPosition.m_latitude = startPosition.m_latitude + latitudeStep; //纬度步长-6
- longtidudeStep += 0.89;
- latitudeStep += 0.35;
- flag++;
- }
- else
- {
- static double longtidudeStep = 2.03;
- static double latitudeStep = 2.26;
- endPosition.flag = false;
- endPosition.m_longtidude = startPosition.m_longtidude + longtidudeStep; //经度步长+5
- endPosition.m_latitude = startPosition.m_latitude - latitudeStep; //纬度步长-6
- longtidudeStep += 0.89;
- latitudeStep += 0.35;
- flag = 1;
- }
- // endPosition.flag = false;
- // endPosition.m_longtidude = startPosition.m_longtidude + 1.35;
- // endPosition.m_latitude = 7.347826*(endPosition.m_longtidude)*(endPosition.m_longtidude)-1563.31770723*(endPosition.m_longtidude)+82873.51;
- //
- }
- //改变导弹的行动轨迹
- void CLmtestView::MissileNextPosition(Position &startPosition, Position &endPosition)
- {
- static int flag = 1; //转弯标识
- if (flag<3)
- {
- static double longtidudeStep = 2.03;
- static double latitudeStep = 2.26;
- endPosition.flag = false;
- endPosition.m_longtidude = startPosition.m_longtidude - longtidudeStep; //经度步长+5
- endPosition.m_latitude = startPosition.m_latitude - latitudeStep; //纬度步长-6
- longtidudeStep += 0.89;
- latitudeStep += 0.35;
- flag++;
- }
- else if(flag <4)
- {
- static double longtidudeStep = 2.03;
- static double latitudeStep = 4.26;
- endPosition.flag = false;
- endPosition.m_longtidude = startPosition.m_longtidude- longtidudeStep; //经度步长+5
- endPosition.m_latitude = startPosition.m_latitude + latitudeStep; //纬度步长-6
- longtidudeStep += 0.89;
- latitudeStep += 0.35;
- flag++;
- }
- else
- {
- static double longtidudeStep = 2.03;
- static double latitudeStep = 2.26;
- endPosition.flag = false;
- endPosition.m_longtidude = startPosition.m_longtidude - longtidudeStep; //经度步长+5
- endPosition.m_latitude = startPosition.m_latitude - latitudeStep; //纬度步长-6
- longtidudeStep += 0.89;
- latitudeStep += 0.35;
- flag = 1;
- }
- }
- //展示飞机的轨迹
- void CLmtestView::ShowPlaneTrace(_planePositionVector &planeVector)
- {
- static int i = 0;
- planeVector[0].flag =TRUE;
- for (i; i<planeVector.size(); i++)
- {
- if (TRUE == planeVector[i].flag)
- {
- //在地图上画点
- DrawPoint(&m_planeLayer,planeVector[i],"FIRE1-32.BMP");
- //设置最新位置点进行加亮
- m_map.GetLayers().Item(2).GetSelection().ClearSelection();
- CMapXFeatures fts = m_map.GetLayers().Item(2).AllFeatures();
- m_map.GetLayers().Item(2).GetSelection().Add(fts.Item(i+1));
- }
- else
- {
- break;
- }
- }
- if (i<planeVector.size())
- {
- planeVector[i].flag = true; //设置下一个标志位
- }
- //画轨迹线
- if (i>1)
- {
- DrawLine(&m_trackLayer, planeVector[i-2], planeVector[i-1],miColorBlue,99);
- }
- }
- //展示导弹的轨迹
- void CLmtestView::ShowMissileTrace(_missilePositionVector &missileVector)
- {
- static int i = 0;
- missileVector[0].flag =TRUE;
- for (i; i<missileVector.size(); i++)
- {
- if (TRUE == missileVector[i].flag)
- {
- //在地图上画点
- DrawPoint(&m_missileLayer,missileVector[i],"MISSILE.BMP");
- //设置最新位置点进行加亮
- m_map.GetLayers().Item(3).GetSelection().ClearSelection();
- CMapXFeatures fts = m_map.GetLayers().Item(3).AllFeatures();
- m_map.GetLayers().Item(3).GetSelection().Add(fts.Item(i+1));
- }
- else
- {
- break;
- }
- }
- if (i<missileVector.size())
- {
- missileVector[i].flag = true; //设置下一个标志位
- }
- //画轨迹线
- if (i>1)
- {
- DrawLine(&m_trackLayer, missileVector[i-2], missileVector[i-1],miColorRed,57);
- }
- }
- //记录飞机的轨迹
- void CLmtestView::RecordPlaneTrace(_planePositionVector &planeVector,Position &positionStart,int count)
- {
- int vectorlength; //记录集合中的元素个数
- Position tempStartPosition; //起始位置
- tempStartPosition = positionStart;
- //将第一个点放入队列
- planeVector.push_back(positionStart);
- while ((vectorlength=planeVector.size()) < count)
- {
- Position tempEndPosition; //结束位置
- PlaneNextPosition(tempStartPosition,tempEndPosition); //得到下一个位置
- planeVector.push_back(tempEndPosition);//将下一个位置放入队列中
- tempStartPosition = tempEndPosition;
- }
- }
- //记录导弹的轨迹
- void CLmtestView::RecordMissileTrace(_missilePositionVector &missileVector,Position &positionStart, int count)
- {
- int vectorlength; //记录集合中的元素个数
- Position tempStartPosition; //起始位置
- tempStartPosition = positionStart;
- //将第一个点放入队列
- missileVector.push_back(positionStart);
- while ((vectorlength=missileVector.size()) < count)
- {
- Position tempEndPosition; //结束位置
- MissileNextPosition(tempStartPosition,tempEndPosition); //得到下一个位置
- missileVector.push_back(tempEndPosition);//将下一个位置放入队列中
- tempStartPosition = tempEndPosition;
- }
- }