3.2.2 使用程序动态地向MapControl中添加图层
注意 : 本代码中 MapControl 控件的 Name 属性 是 aeMapMain
别忘了在单元引用部分添加 esriGeoDatabase_TLB, esriCarto_TLB, esriDataSourcesFile_TLB 和 StrUtils
1 使用常规方法加载地图
1 var
2 pWorkSpaceFactory: IWorkspaceFactory;
3 pFeatureWorkSpace: IFeatureWorkspace;
4 pFeatureClass: IFeatureClass;
5 pFeatureLayer: IFeatureLayer;
6
7 sWorkSpacePath: string;
8 aFileName: array [1 .. 3] of string;
9 sLayerName: string;
10 begin
11
12 sWorkSpacePath := '..\data\Shapefile';//工作空间的路径,应根据你所使用的数据的实际情况设定
13 aFileName[1] := '分市州.shp';
14 aFileName[2] := '县界.shp';
15 aFileName[3] := '地级市人民政府驻地.shp';
16
17 pWorkSpaceFactory := CoShapefileWorkspaceFactory.Create as IWorkspaceFactory;
18 if pWorkSpaceFactory.IsWorkspace(sWorkSpacePath) then //判断工作空间路径的有效性
19 begin
20 pFeatureWorkSpace := pWorkSpaceFactory.OpenFromFile(sWorkSpacePath, 0) as IFeatureWorkspace;
21 for sLayerName in aFileName do
22 begin
23 pFeatureClass := pFeatureWorkSpace.OpenFeatureClass(sLayerName);
24 pFeatureLayer := CoFeatureLayer.Create as IFeatureLayer;
25 pFeatureLayer.Name := LeftStr(sLayerName, Length(sLayerName) - 4);//LeftStr函数位于StrUtils单元,从字符串的左边起取n个字符。
26 pFeatureLayer.FeatureClass := pFeatureClass;
27 aeMapMain.AddLayer(pFeatureLayer, 0);
28 end;
29 end;
30
31 end;
2 pWorkSpaceFactory: IWorkspaceFactory;
3 pFeatureWorkSpace: IFeatureWorkspace;
4 pFeatureClass: IFeatureClass;
5 pFeatureLayer: IFeatureLayer;
6
7 sWorkSpacePath: string;
8 aFileName: array [1 .. 3] of string;
9 sLayerName: string;
10 begin
11
12 sWorkSpacePath := '..\data\Shapefile';//工作空间的路径,应根据你所使用的数据的实际情况设定
13 aFileName[1] := '分市州.shp';
14 aFileName[2] := '县界.shp';
15 aFileName[3] := '地级市人民政府驻地.shp';
16
17 pWorkSpaceFactory := CoShapefileWorkspaceFactory.Create as IWorkspaceFactory;
18 if pWorkSpaceFactory.IsWorkspace(sWorkSpacePath) then //判断工作空间路径的有效性
19 begin
20 pFeatureWorkSpace := pWorkSpaceFactory.OpenFromFile(sWorkSpacePath, 0) as IFeatureWorkspace;
21 for sLayerName in aFileName do
22 begin
23 pFeatureClass := pFeatureWorkSpace.OpenFeatureClass(sLayerName);
24 pFeatureLayer := CoFeatureLayer.Create as IFeatureLayer;
25 pFeatureLayer.Name := LeftStr(sLayerName, Length(sLayerName) - 4);//LeftStr函数位于StrUtils单元,从字符串的左边起取n个字符。
26 pFeatureLayer.FeatureClass := pFeatureClass;
27 aeMapMain.AddLayer(pFeatureLayer, 0);
28 end;
29 end;
30
31 end;
2.使用简单方法加载地图
代码
1 var
2 sWorkSpacePath: string;
3 aFileName: array [1 .. 3] of string;
4 sLayerName: string;
5 begin
6
7 sWorkSpacePath := '..\data\Shapefile';
8 aFileName[1] := '分市州.shp';
9 aFileName[2] := '县界.shp';
10 aFileName[3] := '地级市人民政府驻地.shp';
11
12 for sLayerName in aFileName do
13 begin
14 aeMapMain.AddShapeFile(sWorkSpacePath, sLayerName);
15 end;
16
17 end;
2 sWorkSpacePath: string;
3 aFileName: array [1 .. 3] of string;
4 sLayerName: string;
5 begin
6
7 sWorkSpacePath := '..\data\Shapefile';
8 aFileName[1] := '分市州.shp';
9 aFileName[2] := '县界.shp';
10 aFileName[3] := '地级市人民政府驻地.shp';
11
12 for sLayerName in aFileName do
13 begin
14 aeMapMain.AddShapeFile(sWorkSpacePath, sLayerName);
15 end;
16
17 end;