二、字段集合对象
数据源有多个数据列。MapX管理绑定的每一列数据,因此应当只绑定地图上所需的数据(例如,想要进行专题化地图或标注用的数据)。用DataSets.Add(Fields)的最后一个参数来建立要绑定到地图的字段(列)的Fields集合。
说明:声明字段变量时使用“MapXLib.Fields”。这样可防止与DAO“Fields”对象发生冲突。如:
Dim flds As New MapXLib.Fields
可以通过Dataset.Fields的属性访问数据集的Fields集合。如表6-6所示。
表6-6 Fields集合操作
1.Fields.Add方法
Fields.Add方法使用户得以将数据源中的一列数据作为字段添加到Fields集合中。Fields集合使用DataSets.Add方法建立。Datasets.Add的Fields参数获取Fields集合,并使用Add方法建立它。一旦数据集已经创建,就不能再将Add方法用在数据集的Fields集合上。
Fields.Add的语法规则如下所示:
Fields.Add DataSourceCol,Name,[AggregateFunction],[Type]
Fields.Add方法的各部件描述如表6-7所示。
表6-7 Fields.Add方法的各部件描述
2.数据聚合
Fields.Add方法的AggregateFunction参数决定当多匹配情况出现时MapX如何计算Field的值。
例如,如果数据如表6-8所示,加州有三种销售额,并且只有一个数据与加州相关联,因此就需要告诉MapX要么取销售额的总和,要么取其平均值。
聚合函数Fields.Add用于计算字符列时的默认参数为miAggregationIndividual,用于计算数值列时的默认参数为miAggregationSum。
表6-8是用于处理多匹配情况的聚合,AggregationFunction常量及其功能描述如表6-9所示。
表6-8 数据示例
表6-9 Aggregation Function常量
以下示例程序演示了Fields.Add方法。
Private Const kNumberOfRows=3
Dim theData(2,1)As Variant
'event supported by MapX to build an unbound dataset,which
'allows MapX to access data whose format is known only to
'the programmer.In this example,the data is in the form
'of a two-dimensional array in VB.
Private Sub Form_Load()
theData(0,0)="ME" 'Fill in the data to
theData(1,0)="NH" 'be used by RequestData
theData(2,0)="VT"
theData(0,1)=100
theData(1,1)=200
theData(2,1)=300
Map1.ZoomTo 800,-70.26,44.05 'Zoom in on New England
End Sub
Private Sub Command1_Click()
Dim flds As MapXLib.Fields
Dim ds As Dataset
Set flds=CreateObject("MapXMobile.Fields.5")
'Describe the structure of the unbound dataset:
flds.Add"State","State",miAggregationIndividual,_
miTypeString
flds.Add"Sales","Sales",miAggregationSum,miTypeNumeric
'Create the unbound dataset.The"RequestData"event will be
'triggered to get the data to be used.
Set ds=Map1.DataSets.Add(miDataSetUnbound,Nothing,_
"My Dataset","State",,"USA",flds)
'Create a theme based on the"Sales"column in the unbound dataset
ds.Themes.Add miThemeGradSymbol,"Sales","My Theme"
End Sub
Private Sub Map1_RequestData(ByVal DataSetName As String,_
ByVal Row As Long,ByVal Field As Integer,_
Value As Variant,Done As Boolean)
Done=False
If DataSetName<>"My Dataset"Or Row>kNumberOfRows Then
Done=True
Else
Value=theData(Row-1,Field-1)
End If
End Sub
下面的示例显示了怎样从DAO记录集数据源创建字段集合,以及随后如何用Fields集合向数据集添加数据。
Private Sub cmdOK_Click()
Dim ds As MapXLib.Dataset
Dim rs As RecordSet
Dim i as integer
Dim flds As New MapXLib.Fields
'Open the recordset selected from a list box
Set rs=db.OpenRecordset(DatasetsList.Text)
'Use the AggregationFunction constant selected from a combo box
Dim AggregateTypeNum As Integer
Select Case AggregateType.ListIndex
Case 0
AggregateTypeNum=miAggregationSum
Case 1
AggregateTypeNum=miAggregationAverage
Case 2
AggregateTypeNum=miAggregationCount
Case 3
AggregateTypeNum=miAggregationIndividual
Case 4
AggregateTypeNum=miAggregationAuto
End Select
'Loop through the DAO fields of the Recordse
'and add them to a MapX Fields collection.
For i=0 To rs.Fields.Count-1
flds.Add rs.Fields(i).Name,rs.Fields(i).Name,AggregateTypeNum
Next
'Add the dataset,using the fields collection
Set ds=Map1.Datasets.Add(miDataSetDAO,rs,"US State Data",,,,flds)
MsgBox"Dataset"&DatasetsList.Text&"Added"
Unload Me
End Sub
免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。