为了实现ArcMap中下面的效果,一个图层组GroupLayer中添加几个图层。
ArcEngine 可以通过IGroupLayer来实现,代码如下。
IGroupLayer RouteGroupLayer = new GroupLayerClass();
RouteGroupLayer.Name = "Route Group Layer";
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.Name = "New Layer 1";
RouteGroupLayer.Add(featureLayer);
axMapControl1.AddLayer(RouteGroupLayer, 0);
上面代码实现的效果如下。
现在我们还需要在上面基础上在添加一个图层New Layer 2, 我们可以使用下面的代码。
IGroupLayer groupLayer = new GroupLayerClass();
groupLayer.Name = "New Group Layer";
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.Name = "New Layer 1";
groupLayer.Add(featureLayer);
axMapControl1.AddLayer(groupLayer, 0);
IFeatureLayer featureLayer2 = new FeatureLayerClass();
featureLayer2.Name = "New Layer 2";
groupLayer.Add(featureLayer2);
WinForm框架运行上面代码就是我们想要的结果。
但是,WPF框架运行上面代码的运行效果如下图,发现New Layer 2并没有添加到Route Group Layer中。
后来发现,WPF框架,还需要加上 axTOCControl.Update(); 才会出现我们想要的效果,但是这样,就表示我们的程序中必须添加axTOCControl控件,代码和效果如下。
IGroupLayer groupLayer = new GroupLayerClass();
groupLayer.Name = "Group Layer";
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.Name = "New Layer 1";
groupLayer.Add(featureLayer);
axMapControl1.AddLayer(groupLayer, 0);
IFeatureLayer featureLayer2 = new FeatureLayerClass();
featureLayer2.Name = "New Layer 2";
groupLayer.Add(featureLayer2);
axTOCControl.Update(); // 后添加代码
WPF框架,如果不想添加axTOCControl控件,实现插入New Layer 2就要借助 IMapLayers.InsertLayerInGroup方法,代码如下。
IGroupLayer groupLayer = new GroupLayerClass();
groupLayer.Name = "Group Layer";
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.Name = "New Layer 1";
groupLayer.Add(featureLayer);
axMapControl.AddLayer(groupLayer, 0);
IFeatureLayer featureLayer2 = new FeatureLayerClass();
featureLayer2.Name = "New Layer 2";
IMapLayers mapLayers = axMapControl.Map as IMapLayers;
//第一个是GroupLayer的图层,第二个参数是要插入到组中的图层,第四个参数是要插入的位置, 0为首位
mapLayers.InsertLayerInGroup(groupLayer, featureLayer2, true, 0);