AE接口解析-IMAP
IMAP接口提供用的方法是用来控制地图的,AE 、AO均提供该接口。当需要在地图上显示的数据进行操作时你也许该从它提供的方法中找答案。在ArcGIS开发帮助中关于IMAP接口的使用说明入下:Use the IMap interface to display data from various data sources. The IMap interface is a starting point for many of the tasks one does with a Map. For example, use IMap to add, delete, and access map layers containing data from various sources including feature layers and graphics layers; associate map surround objects (legends, scale bars, etc) with the Map; access the various properties of a Map including the area of interest, the current map units, and the spatial reference; select features and access the Map's current selection.
“The IMap interface is a starting point for many of the tasks one does with a Map.”意思是一幅地图大部分的工作都是从IMAP接口开始的,可见此接口的重要性,所以就在它开始吧!亲
首先来说的是layer三剑客:layer、 layers、 layercount
layer:Layer(Index)其是一个只读属性返回指定索引index位置的Layer,使用如下例:
public int LayerCount(AxMapControl amc1)
{
IMap pmap = amc1.Map;
int n = pmap.LayerCount;
return n;
}
layers:Layers (uid, recursive)是一个只可读不可写的属性,当第二个属性为true时该属性获取第一个参数uid指定的Layers,赋值给一个IEnumLayer的变量,开发帮助解释如下:The layers in the map of the type specified in the uid. If recursive is true it will return layers in group layers.
使用可看下面一个简单的例子:
public void GetFeatureLayers(AxMapControl amc1)
{
IMap pmap = amc1.Map;
ILayer player;
IEnumFeature pEnumfeature;
UID puid = new UID();
puid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
pEnumfeature =(IEnumFeature)pmap.get_Layers(puid,true);
pEnumfeature.Reset();
player = (ILayer)pEnumfeature.Next();
do
{
MessageBox.Show(player.Name);
player = (ILayer)pEnumfeature.Next();
}
while (player != null);
}
其中比较常用的UID参数值如下:
{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer
{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
{EDAD6644-1810-11D1-86AE-0000F8751720} IgroupLayer
LayerCount:LayerCount其是一个只读属性返回指定map里面Layer的个数,使用如下例
:
public void LayerCount(AxMapControl amc1, ref int n)
{
IMap pmap = amc1.Map;
n = pmap.LayerCount;
}
SelectionCount属性:是个只读,返回一个int类型为map被选中要素的个数,如下例:
IMap pmap = amc1.Map;
int m = pmap.SelectionCount;
MapScale属性:可读可写,double类型,获取或者设置当前map的地图比例尺,如下例:
pmap.MapScale =double m;\\设置
double m=pmap.MapScale;\\获取
接下来是几个常用的IMap接口的方法:
AddLayer方法:AddLayer(Ilayer Layer)向该map添加一个Layer。该方法不可重载只有一个Ilayer接口的实例。注意,几乎每一个地图都是按照图层来组织的,图形的存储就好像是一个堆栈结构,也就是说你最后添加的图层,他的图层编号是0,这个的确有些怪异。有趣的是在IMapControl接口中也提供了一个Addlayer方法,其可以重载。与Imap方法不同的是它有两个参数的方法:AddLayer (ILayer Layer,int toIndex)它可以让我们指定图层号。
AddLayers方法:public void AddLayers (IEnumLayer Layers,bool autoArrange),添加一个EnumLayer变量的layers到该map,第一个参数为IEnumLayer类型,第二个参数为bool型变量。要说明的是如果参数 autoArrange为true,加入的图层是可以自动排序的,默认情况下注记层在最上面,然后依次是点层、线层、面层。还有它还会尝试自动建立地图的空间坐标系哎:)。
ClearLayers方法:如果你想将所有的layer从map中移除,用它吧!
ClearSelection方法:如果你想清除你已选择的要素,用它吧!
SelectFeature 方法:public void SelectFeature (ILayer Layer,IFeature Feature)从一个Layer中选择一个Feature。
MoveLayer方法:public void MoveLayer(ILayer Layer, int toIndex)把一个Layer从当前的位置移动到指定的索引位置。如下例:
public void Movelayer(AxMapControl amc1, ILayer pLayer,int n)
{
IMap pmap = amc1.Map;
pmap.MoveLayer(pLayer, n);
}
SelectByShape方法:public void SelectByShape (Shape, env, justOne )方法是选择Map中的和指定的shape相交的所有FeatureLayer图层。只有IFeatureLayer::Selectable设置为true时图层才会被搜索,env参数是设置选择环境的,可以自定义一个环境或者直接传入nothing,justone参数是指是否在选择到一个实体就停止搜索,选择的结果保存在每个featurelayer的选择集中,可看下例:
利用SelectByShape方法,实现矩形选择要素,并闪烁、定位。
IMap pMap = new MapClass();
pMap = axMapControl1.Map;
IPoint pPt = new PointClass(); ;
pPt.PutCoords(e.mapX, e.mapY);
IEnvelope pEn = new EnvelopeClass();
pEn = axMapControl1.TrackRectangle();
pMap.SelectByShape(pEn, null, false);//最后一个参数控制是否只选一个
axMapControl1.Refresh();
IEnumFeature pEnumFeature = pMap.FeatureSelection as IEnumFeature;
IFeature pFeature;
pFeature = pEnumFeature.Next();
//选中闪烁,并视图定位
while (pFeature != null)
{
Application.DoEvents();
pEn.Union(pFeature.Extent);
axMapControl1.FlashShape(pFeature.Shape, 3, 500, null);
pFeature = pEnumFeature.Next();
}
axMapControl1.ActiveView.Extent = pEn;