创建地图对象
必要:创建地图对象,必须使用FeatureFactory对象
1、 创建一个点对象
点对象有一个坐标点(X,Y),点对象变量是Point类型,点对象的样式(Style)是符号样式。
Dim Pnt AS MapXLib.Point
Dim FeaFaac AS MapXLib.FeatureFactory
Dim Lyr AS MapXLib.Layer
Dim Ftr AS MapXLib.Feature
Dim NewStyle AS MapXLib.Style
‘绑定
SET Lyr=MainMap.Layers.Item(LayerName)
SET FeaFac=mainmap.featurefactory
‘设置点对象样式
With NewStyle
.SymbolType = miSymbolTypeBitmap
.SymbolBitmapSize = 24
.SymbolBitmapTransparent = False
.SymbolBitmapName = "YIEL2-32.BMP"
End With
Mainmap.AutoRedraw=False ‘禁止自动刷新
Lyr.Editable=True ‘置当前图层为可写状态
‘创建点对象
pnt.set X1,Y1
‘添加进当前图层
Set Ftr=FeaFac. CreateSymbol (Pnt,Newstyle) ‘创建符号
‘Set Ftr=FeaFac. CreateSymbol (Pnt,MainMap.DefaultStyle)
‘添加
Lyr.AddFeature Ftr
Lyr.Refresh
Mainmap.AutoRedraw=True
Lyr.Editable=False
‘释放
SET Pnt = Nothing
SET FeaFac = Nothing
SET Lyr = Nothing
SET Ftr = Nothing
‘以上代码放在MapX的ToolUsed事件下
单独修改某个图元的样式:SET Ftr.Style=NewStyle,再用Update 即可
2、 创建一个线矩形
Dim Pnts AS MapXLib.Points
With NewStyle
.LineColor=Rgb(0, 0,255)
End With
‘第一个点
Pnt.Set X1,Y1
Pnts.add Pnt
‘第二个点
Pnt.Set X2,Y1
Pnts.add Pnt
‘第三个点
Pnt.Set X2,Y2
Pnts.add Pnt
‘第四个点
Pnt.Set X1,Y2
Pnts.add Pnt
‘第五个点
Pnt.Set X1,Y1
Pnts.add Pnt
‘创建线矩形
SET Ftr=FeaFac.CreateLine(Pnts,NewStyle)
Lyr.AddFeature Ftr
Lyr.Refresh
3、 上面创建对象中存在的问题:并未对其数据数据进行赋值创建对象的同时创建其数据集合
Dim Pnt AS MapXLib.Point
Dim FeaFac AS MapXLib.FeatureFactory
Dim Lyr AS MapXLib.Layer
Dim Ftr AS MapXLib.Feature
Dim NewStyle AS MapXLib.Style
Dim ds AS MapXLib.Dataset
Dim Flds AS MapXLib.Fields
‘绑定
SET Lyr=MainMap.Layers.Item(LayerName)
SET ds=Lyr.Datasets.Item(1)
Set Flds=ds.Fields
SET FeaFac=mainmap.featurefactory
‘设置点对象样式
With NewStyle
.SymbolType = miSymbolTypeBitmap
.SymbolBitmapSize = 24
.SymbolBitmapTransparent = False
.SymbolBitmapName = "YIEL2-32.BMP"
End With
Mainmap.AutoRedraw=False ‘禁止自动刷新
Lyr.Editable=True ‘置当前图层为可写状态
‘创建点对象
pnt.set X1,Y1
‘创建图形
Set Ftr=FeaFac. CreateSymbol (Pnt,Newstyle) ‘创建符号
‘Set Ftr=FeaFac. CreateSymbol (Pnt,MainMap.DefaultStyle)
‘设置属性
For I=1 to Flds.Count
Lyr.KeyFields=Flds.Item(i).Name
Ftr.KeyValue=ValueStr(I) ‘这里并没有对字段类型进行判断
Next
‘另外一种方法:使用RowValues和RowValue对象
‘添加
Lyr.AddFeature Ftr
Lyr.Refresh
Mainmap.AutoRedraw=True
Lyr.Editable=False
‘释放
SET Pnt = Nothing
SET FeaFac = Nothing
SET Lyr = Nothing
SET Ftr = Nothing
SET ds = Nothing
SET Flds = Nothing
4、 创建表
(1) 临时表:
A、用MainMap.Layers.CreateLayer方法创建临时表。但这个临时表只有一个字段:GeoNa
me( Char 24)。程序运行过程中该表存放位置为系统临时文件夹下
B、使用LayerInfo对象创建临时表,可以指定字段。示例:
Dim Lyr As MapXLib.Layer
Dim LayerInfo As New MapXLib.LayerInfo
Dim Flds As New MapXLib.Fields
‘字段定义
Flds.AddStringField "ID", 12
Flds.AddStringField "Name", 50
Flds.AddNumericField "Deptch", 12, 2
Flds.AddIntegerField "Length"
´
LayerInfo.Type = miLayerInfoTypeTemp
LayerInfo.AddParameter "FileSpec", FileName
LayerInfo.AddParameter "NAME", LayerName
LayerInfo.AddParameter "Fields", Flds
Set Lyr = MainMap.Layers.Add(LayerInfo, 1)
Set Lyr = Nothing
Set LayerInfo = Nothing
(2) 创建永久表
Dim Lyr As MapXLib.Layer
Dim LayerInfo As New MapXLib.LayerInfo
Dim Flds As New MapXLib.Fields
Flds.AddStringField "ID", 12
Flds.AddStringField "Name", 50
Flds.AddNumericField "Deptch", 12, 2
Flds.AddIntegerField "Length"
´
LayerInfo.Type = miLayerInfoTypeNewTable
LayerInfo.AddParameter "FileSpec", FilePath + "" + FileName
LayerInfo.AddParameter "NAME", LayerName
LayerInfo.AddParameter "Fields", Flds
Set Lyr = MainMap.Layers.Add(LayerInfo, 1)
Set Lyr = Nothing
Set LayerInfo = Nothing
5、 创建工具句柄
系统已经定义工具句柄都以整数(包括16进制)常数存在,句柄号大于1000和小于12基本都为系统使用。
A. 定义常数:必须为全局变量
Global Const CreateSymbolTool = 13 ´创建节点
Global Const CreateLineTool = 15 ´创建管线
Global Const InfoTipTool = 16 ´信息工具
Global Const MoveFeature = 17 ´移动地图
Global Const ScaleDistanceTool = 18 ´测量两点间的距离
B. 使用CreateCustomTool创建新的工具句柄:
MainMap.CreateCustomTool CreateSymbolTool, miToolTypePoint, miSymbolCursor
MainMap.CreateCustomTool CreateLineTool, miToolTypeLine, miCrossCursor
MainMap.CreateCustomTool InfoTipTool, miToolTypePoint, miCrossCursor
MainMap.CreateCustomTool MoveFeature, miToolTypeLine, miPanCursor
MainMap.CreateCustomTool ScaleDistanceTool, miToolTypeLine, miPanCursor
C. 如何使用?
在Map对象的ToolUsed事件的ToolNum参数为当前所激活的工具
使当
前操作指向某行为:MainMap.CurrentTool=工具句柄号,如放大:MainMap.CurrentTool=
miZoomInTool,移动图元:MainMap.CurrentTool=MoveFeature
操作具体的工具句柄时,执行该捕捉到的工具句柄的代码:
在ToolUsed事件中:
Select Case ToolNum
Case MoveFeature
‘执行代码
End Select