现在的位置: 首页 > 综合 > 正文

VC+MAPX 属性数据的读取

2013年08月21日 ⁄ 综合 ⁄ 共 1892字 ⁄ 字号 评论关闭
char clientpath[121];
GetCurrentDirectory(120,clientpath);//得到当前路径
CString
m_tempclientpath =
_T("");
m_tempclientpath.Format("%s/map/GPS_status.tab",clientpath);    //地图所在的目录放置tab文件
CString
tempPathGST=_T("");
tempPathGST.Format("%s/map/Untitled.gst",clientpath);    //取得地图集gst文件路径
CMapXFields
Flds;
if(!Flds.CreateDispatch(Flds.GetClsid ()))
//return
FALSE;
    MessageBox("Create Field error!");
VARIANT
FieldName;                     //字段名称
FieldName.vt = VT_BSTR;
CString
csTmp;         //中文字段名称
csTmp = _T("StatusField");
LPCTSTR lcsTmp =
(LPCTSTR)csTmp;
VARIANT
FieldLength;                  //字段长度
FieldLength.vt=VT_I2;
FieldLength.iVal
=200;
long iwidth =
200;
Flds.AddStringField(lcsTmp,iwidth,false);
//Create a variant that
points to the fields collection just created
VARIANT vFlds;
vFlds.vt =
VT_DISPATCH;
vFlds.pdispVal = Flds.m_lpDispatch;
//Create a new LayerInfo
Object
CMapXLayerInfo
Linfo;
Linfo.CreateDispatch(Linfo.GetClsid());
//Set the Layer Info type
to type: New Table
Linfo.SetType(miLayerInfoTypeNewTable);
//Add the
parameters for the new table
Linfo.AddParameter("FileSpec",
COleVariant(m_tempclientpath)); //Tab file
Linfo.AddParameter("Name",
COleVariant("GPS_status")); //Layer name
Linfo.AddParameter("Fields", vFlds);
//Fields
Linfo.AddParameter("OverwriteFile", COleVariant("1"));
//Add the
new layer to the map

CMapXLayer
lyr=m_CtrlMapX.GetLayers().Add(Linfo.m_lpDispatch);
MessageBox("Create Layer
correctly,StatusLayer!");
//下面代码为了显示所有图层的名字,检验是否添加了新的图层
CMapXLayers
layers=m_CtrlMapX.GetLayers();
int iii=layers.GetCount();
if
(iii>0)
{      
        for (int iss=1; iss <= iii; iss
++)
        {
            CMapXLayer
templayer=layers.Item(iss);
            CString
sss=templayer.GetName();
            AfxMessageBox(sss);
        }
}
//save
the gst
file
//如果不储存gst,那么下次打开的时候还是没有将这个图层添加到gst文件中去
//m_CtrlMapX.SaveMapAsGeoset("",tempPathGST);

第二种方法:已经有了图层文件,只需将文件添加到地图集中去
其他代码参照第一个例子
取得tab文件的路径在m_tempclientpath中,添加图层
CMapXLayer
lyr=m_CtrlMapX.GetLayers().Add(m_tempclientpath);
记得最后要保存gst文件,否则下次打开还需要添加。

抱歉!评论已关闭.