例程参见DET中的CMapXOP类和CRoadAttrCompReplaceDlg类。
1. 创建控件
if ( !CMapX::Create( lpszWindowName , dwStyle , rect ,pParentWnd , nID , pPersist , bStorage , bstrLicKey ) )
return FALSE;
CMapX::SetMapUnit( miUnitMeter );
CMapX::SetAreaUnit( miUnitSquareMeter );
CMapX::SetDefaultConversionResolution(100);
CMapX::SetMousewheelSupport( miFullMousewheelSupport );
CMapX::GetLayers().RemoveAll();
CMapXCoordSys cs = CMapX::GetDisplayCoordSys();
double y = cs.GetOriginLatitude();
double x = cs.GetOriginLongitude();
short s = cs.GetType();
CMapXRectangle Rect;
CMapXCoordSys CurrentCoordsys;
COptionalVariant vEmpty;
VARIANT MapUnit,MapBounds;
if(!Rect.CreateDispatch(Rect.GetClsid()))
{
TRACE0("Could not Create object");
}
if(!CurrentCoordsys.CreateDispatch(CurrentCoordsys.GetClsid()))
{
TRACE0("Could not Create object");
}
MapUnit.vt=VT_I4;
MapUnit.lVal=miUnitMeter;
//设置坐标范围(MBR).为求一致,这里取当GeosetManager中设置为non-earth(meters)时,
//gst文件中的参数"\GEOSET\MBR\LOWERLEFT" ,"\GEOSET\MBR\UPPERRIGHT"。
Rect.Set( 0,0,180,90 );
MapBounds.vt=VT_DISPATCH;
MapBounds.pdispVal=Rect.m_lpDispatch;
MapBounds.pdispVal->AddRef();
// 经纬度投影
CMapXDatum datum;
datum.CreateDispatch( datum.GetClsid() );
datum.Set( 28,0,0,0,0,0,0,0,0 );
CurrentCoordsys.Set( miLongLat, datum,MapUnit,
vEmpty, vEmpty, vEmpty,vEmpty, vEmpty,
vEmpty, vEmpty, vEmpty, vEmpty,MapBounds, vEmpty);
CMapX::SetNumericCoordSys(CurrentCoordsys.m_lpDispatch);
CMapX::SetDisplayCoordSys(CMapX::GetNumericCoordSys().m_lpDispatch);
CMapX::SetTitleText( _T("") );
2.加载图层
CMapXLayer lyrTemp = CMapX::GetLayers().Add( tcsTbFile );
VARIANT vr;
vr.vt = VT_DISPATCH;
vr.pdispVal = lyrTemp;
CMapX::GetDatasets().Add( miDataSetLayer , vr , lyrTemp.GetName() );
3.卸载图层
m_pMapXOP->GetDatasets().Remove(_T("navi_arc"));
m_pMapXOP->GetLayers().Remove(_T("navi_arc"));
4.读取属性值
(1) 整型字段值
//cd traffic flow
VARIANT var = rowvlus_cd.Item(_T("TrafficFlo")).GetValue();
int iTrfcFlw = var.dblVal;
(2)字符串型字段值
//sw direction
VARIANT var1 = rowvlus_sw.Item(_T("Direction")).GetValue();
string strdir = m_StringOp.ConverVariantToMBCS(var1);
5.更新属性值
(1) 获取到要更新的图层的rowvalues
CMapXRowValues rowvlus_cd = dstNavi_cd.GetRowValues(k);
(2)更新相应的字段值
rowvlus_cd.Item(_T("TrafficFlo")).SetValue( COleVariant(long(iTrfcFlw)) );
ftr_cd.Update( true, rowvlus_cd );
6.创建新表
//创建空字段对象
CMapXFields Flds;
if(!Flds.CreateDispatch(Flds.GetClsid()))
{
TRACE0("failed to create Flds");
return 0;
}
//根据字段类型在空对象中加入字段
switch( fieldType )
{
case miTypeString:
{
int width = appendField.fieldWidth;
Flds.AddStringField( lpszFileName , width );
break;
}
case miTypeNumeric:
{
Flds.AddNumericField( lpszFileName , 11 , 2 );
break;
}
case miTypeDate:
{
Flds.AddDateField( lpszFileName );
break;
}
case miTypeInteger:
case miTypeSmallInt:
{
Flds.AddIntegerField( lpszFileName );
break;
}
case miTypeFloat:
{
Flds.AddFloatField( lpszFileName );
break;
}
case miTypeLogical:
{
Flds.AddLogicalField( lpszFileName );
break;
}
CMapXLayerInfo LayerInfo;
if(!LayerInfo.CreateDispatch(LayerInfo.GetClsid()))
{
TRACE0("Failed to create LayerInfo");
return 0;
}
LayerInfo.SetType( miLayerInfoTypeNewTable );
lpszFileName = pStringOp->ConvertMcbsToWideChar( lpBuffer );
LayerInfo.AddParameter( _T("FileSpec"),COleVariant( lpszFileName ));
lpszFileName = pStringOp->ConvertMcbsToWideChar( ptcsElemName );
LayerInfo.AddParameter(_T("Name"),COleVariant( lpszFileName ) );
VARIANT vtFlds;
vtFlds.vt = VT_DISPATCH;
vtFlds.pdispVal = Flds.m_lpDispatch;
LayerInfo.AddParameter(_T("Fields"),vtFlds);
//UnloadMap();
lyr = CMapX::GetLayers().Add(LayerInfo.m_lpDispatch);
vr.pdispVal = lyr;
CMapX::GetDatasets().Add( miDataSetLayer,vr,lpszFileName );
7.紧缩表
(1)、将以下申明加入例子自带的MapX.h,位置在CMapXLayer类申明的Operations之Public:中即可
BOOL SupportsPack(long PackType);//sdy080520
void Pack(long PackType);//sdy080520
(2)、将以下声明加入例子自带的MapX.h的对应位置(最上边有一处是专门声明enum的,放在那个位置即可)
enum LayerPackConstants
{
miPackGraphics = 1,
miRebuildGraphics = 2,
miPackIndex = 4,
miPackRebuildIndex = 8,
miPackData = 16,
miPackCompactDB = 32,
miPackAll = 21 //miPackGraphics & miPackIndex & miPackData;
}; //sdy080520
(3)、将以下代码加入例子自带的MapX.cpp
BOOL CMapXLayer::SupportsPack(long PackType)//sdy080520
{
BOOL result;
static BYTE parms[] =
VTS_I4;
InvokeHelper(0x35, DISPATCH_METHOD, VT_BOOL, (void*)&result, parms,
PackType);
return result;
}
void CMapXLayer::Pack(long PackType)//sdy080520
{
static BYTE parms[] =
VTS_I4;
InvokeHelper(0x36, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
PackType);
}
(4)、使用方式
CMapXLayer layer = m_ctrlMapX.GetLayers().Item(index);
layer.Pack(miPackAll);