CMapX m_ctrlMapX;
CMapXLayer m_objLayer;//运动目标图层
CMapXLayer m_traceLayer;//目标轨迹层
CMapXLayer m_scaleBarLayer;//比例尺图层
CMapXFeature m_objFt;
double m_lfCenterLon;//地图中心经度
double m_lfCenterLat;//地图中心纬度
CMapXFeature m_objTrackFt[MAXNUMINEMAP];
1 创建地图控件
CRect rc;
GetClientRect(&rc);
m_ctrlMapX.Create(NULL,WS_VISIBLE, rc, this, IDC_MAP);
m_ctrlMapX.SetCurrentTool(miPanTool);
char GEOName[MAX_PATH];
GetCurrentDirectory(MAX_PATH,GEOName);
strcat(GEOName,"/map/asia.gst");
m_ctrlMapX.SetGeoSet(GEOName);//设置地图名字
m_ctrlMapX.SetTitleText("");
m_ctrlMapX.SetMapUnit(miUnitKilometer);//设置地图单位
m_ctrlMapX.SetPaperUnit(miPaperUnitKilometer);
m_scaleBarLayer = m_ctrlMapX.GetLayers().AddUserDrawLayer("比例尺", 1);//添加用户显示图层
m_ctrlMapX.SetAutoRedraw(TRUE);
2 创建目标轨迹层
m_objLayer = m_ctrlMapX.GetLayers().CreateLayer("目标",NULL, 1, 32);
m_ctrlMapX.GetLayers().SetAnimationLayer(m_objLayer);//设置该层为动画图层
m_objLayer.SetSelectable(FALSE);//设置该层目标为不可选择
m_objLayer.SetAutoLabel(TRUE);//设置该层目标为自动标注
m_objLayer.GetLabel().SetLineType(miLineTypeNone); //设置标注线形
m_objLayer.GetLabel().SetPosition(miPositionTR); //设置标注位置
m_objLayer.GetLabel().SetOffset(2); //设置标注偏移量
m_objLayer.SetOverrideStyle(TRUE);//该层中所有Feature使用相同的style,***关键点,使用不同的style时,参数为FALSE***
m_objLayer.GetLabel().GetStyle().SetTextFontColor(miColorRed);//设置标注颜色
m_objLayer.GetLabel().SetOverlap(TRUE); //允许标注覆盖
m_objLayer.GetStyle().GetSymbolFont().SetName("Mapinfo Symbols");//设置符号feature使用的字体
m_objLayer.GetStyle().SetSymbolCharacter(53); //设置符号feature使用的字符
m_objLayer.GetStyle().SetSymbolFontColor(RGB(255,0,0)); //设置符号feature使用的颜色
m_objLayer.GetStyle().SetLineWidth(1); //设置线形feature的线宽
m_objLayer.SetDrawLabelsAfter(TRUE); //在所有图层绘制完后再绘制标注
3 创建目标轨迹层
m_traceLayer = m_ctrlMapX.GetLayers().CreateLayer("轨迹",NULL, 2, 32);
m_ctrlMapX.GetLayers().SetAnimationLayer(m_traceLayer);
m_traceLayer.SetSelectable(FALSE);
m_traceLayer.SetDrawLabelsAfter(TRUE);//影响标注刷新的关键
4 响应OnDrawUserLayer绘制比例尺图层
可以参见Mapinfo工具自带的例子程序
5 定时器中改变目标位置
6 刷新电子地图
m_objLayer.BeginAccess(miAccessReadWrite);//大大减少CPU开销
m_traceLayer.BeginAccess(miAccessReadWrite);//大大减少CPU开销
for(int i=0;i<MAXNUMINEMAP; i++)
{
//设定目标位置
sObjectInEMap[i].ft.GetPoint().Set(sGISPoint[i][0].x, sGISPoint[i][0].y);
sObjectInEMap[i].ft.Update();
//设置目标轨迹
//为空的时候创建目标轨迹对象
if (m_objTrackFt[i]==NULL)
{
CMapXFeature ft;
ft.CreateDispatch(ft.GetClsid());
ft.Attach(m_ctrlMapX.GetDispatch(FALSE));
ft.SetType(miFeatureTypeLine);
ft.GetStyle().SetLineColor(colors[i%12]);
ft.GetStyle().SetLineStyle(1);//77-铁轨(参考帮助)
ft.GetStyle().SetLineWidth(2);
CMapXPoints pts;
pts.CreateDispatch(pts.GetClsid());
//加入坐标数据
pts.AddXY(sGISPoint[i][1].x, sGISPoint[i][1].y);
pts.AddXY(sGISPoint[i][0].x, sGISPoint[i][0].y);
ft.GetParts().Add(pts);
//加入到目标图层
m_objTrackFt[i] = m_traceLayer.AddFeature((LPDISPATCH)ft);
}
else
{
//更新目标轨迹对象
m_objTrackFt[i].GetParts().Item(1).AddXY(sGISPoint[i][0].x, sGISPoint[i][0].y);
m_objTrackFt[i].Update();
}
sGISPoint[i][1] = sGISPoint[i][0];
}
//为空的时候创建目标轨迹对象
if (m_objTrackFt[i]==NULL)
{
CMapXFeature ft;
ft.CreateDispatch(ft.GetClsid());
ft.Attach(m_ctrlMapX.GetDispatch(FALSE));
ft.SetType(miFeatureTypeLine);
ft.GetStyle().SetLineColor(colors[i%12]);
ft.GetStyle().SetLineStyle(1);//77-铁轨(参考帮助)
ft.GetStyle().SetLineWidth(2);
CMapXPoints pts;
pts.CreateDispatch(pts.GetClsid());
//加入坐标数据
pts.AddXY(sGISPoint[i][1].x, sGISPoint[i][1].y);
pts.AddXY(sGISPoint[i][0].x, sGISPoint[i][0].y);
ft.GetParts().Add(pts);
//加入到目标图层
m_objTrackFt[i] = m_traceLayer.AddFeature((LPDISPATCH)ft);
}
else
{
//更新目标轨迹对象
m_objTrackFt[i].GetParts().Item(1).AddXY(sGISPoint[i][0].x, sGISPoint[i][0].y);
m_objTrackFt[i].Update();
}
sGISPoint[i][1] = sGISPoint[i][0];
}
m_traceLayer.EndAccess();
m_objLayer.EndAccess();