C # layer operations for MapX Secondary Development
In particular, this article is based on Network MapX reads data from the database to form a new layer (C #)
Implement MapX slave database in C # Read data There are two problems in creating a new layer:
1. MapX reads data from the database to form a new layer;
2. Convert the datatable to the recordset of ADO. The second problem is caused by the first one, because MapX is a COM control and only supports the data access method of ADO. in C # programming, ADO is generally used. net method. Therefore, you need to perform a conversion between the two methods. (Of course, you can also use ADO in C)
The operation code for datatable to convert to ADO recordset is as follows.
/// <Summary>
/// In. net, ADO. NET is used to replace ADO for data access. However, some COM controls only support ADO and do not support ADO. net.
/// To use this type of control, only data objects in ADO. Net can be converted to recordset In ADO.
/// (The DataSet object is essentially a collection of datatable. Therefore, this article only describes the conversion of datatable objects ).
/// </Summary>
Public sealed class adonettoado
{
/// <Summary>
/// Convert a able object to a recordeset object
/// </Summary>
/// <Param name = "table"> datatable object </param>
/// <Returns> the converted recordeset object </returns>
Public static recordset convertdatatabletorecordset (datatable table)
{
// Train of thought:
// 1. After creating A recordset object, create a field in the column corresponding to the datatable. Therefore, you need to convert the Data Type of ADO. net to the Data Type of ADO;
// 2. Open the recordset object, corresponding to each row in the datatable object, create a record in the recordset object, and assign values to each field.
Recordset rs = new RecordsetClass();
foreach (DataColumn dc in table.Columns)
{
rs.Fields.Append(dc.ColumnName, GetDataType(dc.DataType), -1, FieldAttributeEnum.adFldIsNullable, Missing.Value);
}
rs.Open(Missing.Value, Missing.Value, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, -1);
foreach (DataRow dr in table.Rows)
{
rs.AddNew(Missing.Value, Missing.Value); object o;
for (int i = 0; i < table.Columns.Count; i++)
{
rs.Fields[i].Value = dr[i];
o = rs.Fields[i].Value;
}
}
return rs;
}
/// <Summary>
/// Convert the ADO. NET data type to the ADO Data Type
/// </Summary>
/// <Param name = "datatype"> data type of ADO. Net </param>
/// <Returns> ADO Data Type </returns>
Private Static datatypeenum getdatatype (type datatype)
{
Switch (datatype. tostring ())
{
Case "system. boolean": Return datatypeenum. adboolean;
Case "system. Byte": Return datatypeenum. adunsignedtinyint;
Case "system. Char": Return datatypeenum. adchar;
Case "system. datetime": Return datatypeenum. addate;
Case "system. Decimal": Return datatypeenum. addecimal;
Case "system. Double": Return datatypeenum. addouble;
Case "system. int16": Return datatypeenum. adsmallint;
Case "system. int32": Return datatypeenum. adinteger;
Case "system. int64": Return datatypeenum. adbigint;
Case "system. sbyte": Return datatypeenum. adtinyint;
Case "system. Single": Return datatypeenum. adsingle;
Case "system. String": Return datatypeenum. advarchar;
// Case "timespan": Return datatypeenum.
Case "system. uint16": Return datatypeenum. adunsignedsmallint;
Case "system. uint32": Return datatypeenum. adunsignedint;
Case "system. uint64": Return datatypeenum. adunsignedbigint;
Default: Throw (new exception ("no corresponding data type "));
}
}
}
After obtaining the recordset object, how can we solve the first problem. The procedure is as follows:
1. Create a cmapxfields object and Field Add fields;
2. Create a cmapxbindlayer object and specify the serial number of its coordinate value field;
3. Add a dataset to map. datasets to generate a new layer;
4. Specify the display style of elements in the new layer. Method To this end, place the bitmap to be displayed in the custsymb folder of the installation directory of MapX.
The specific operation code is as follows:
/// <Summary>
/// Delete All layer data
/// </Summary>
/// <Param name = "layername"> </param>
Private void deletelayerbyname (string layername)
{
// The serial number of the layer starts from 1.
Int COUNT = axmap1.layers. count;
For (INT I = 1; I <count; I ++)
{
If (axmap1.layers [I]. Name = layername)
{
Axmap1.layers. Remove (I );
}
}
}
/// <Summary>
/// Create a new layer
/// </Summary>
/// <Param name = "layername"> </param>
/// <Param name = "rsnopass"> </param>
Private void creatnewlayerfromdb (string layername, ADODB. recordset rsnopass)
{
Deletelayerbyname (layername); // Delete the original layer
Cmapxfields FLDS = new fieldsclass ();
// Describe the structure of the unbound Dataset
FLDS. Add ("stationid", "theid", aggregationfunctionconstants. miaggregationindividual,
Fieldtypeconstants. mitypestring );
FLDS. Add ("Address", "Address", aggregationfunctionconstants. miaggregationindividual,
Fieldtypeconstants. mitypestring );
FLDS. Add ("longpolling", "longpolling", aggregationfunctionconstants. miaggregationsum,
Fieldtypeconstants. mitypenumeric); // longitude
FLDS. Add ("latitude", "latitude", aggregationfunctionconstants. miaggregationsum,
Fieldtypeconstants. mitypenumeric); // latitude
CMapXBindLayer bindLayerObject = new BindLayerClass();
bindLayerObject.LayerName = layerName;
bindLayerObject.RefColumn1 = 3;
bindLayerObject.RefColumn2 = 4;
bindLayerObject.LayerType = BindLayerTypeConstants.miBindLayerTypeXY;
CMapXDataset dataSet = axMap1.DataSets.Add(DatasetTypeConstants.miDataSetADO, rsNoPass, layerName, "stationid", "address", bindLayerObject, flds, false);
CMapXLayer layer = axMap1.Layers._Item(layerName);
layer.OverrideStyle = true;
string picName = "icon.BMP";
if (layer.Style.SupportsBitmapSymbols == true)
{
layer.Style.SymbolType = SymbolTypeConstants.miSymbolTypeBitmap;
layer.Style.SymbolBitmapSize = 60;
layer.Style.SymbolBitmapTransparent = true;
layer.Style.SymbolBitmapName = picName;
}
}