arcEngine开发中,经常面临一个情况,就是查询到一个要素或指定一个要素的id,去让这个要素高亮,其实实现这个功能很简单,只不过基于arcEngine二次开发,需要熟悉它的官方文档才能找到这个方法,这里简单记录一下
不管是什么方法,都需要利用用户传递的参数查到这个要素,如用户传来一个唯一属性,用户指定某个查询条件等,这里情况太多,不多说,这里简单说一下拿到要素之后的操作
? 该类有一个select集合,通过追加要素到这个集合,再下次刷新时,会将集合里的要素高亮,同时,这个集合提供了添加和清除方法。
private void button1_Click(object sender, EventArgs e)
{
int randomNumbers = 0;
IFeatureLayer featureLayer = (IFeatureLayer)this.axMapControl1.Map.get_Layer(randomNumbers);
IFeatureClass featureClass = featureLayer.FeatureClass;
IActiveView activeView = (IActiveView)this.axMapControl1.Map;
// 获取当前活动视图中的地图对象
IMap map = activeView.FocusMap;
// 清除地图上的所有选择
map.ClearSelection();
//随机生成一个OID,0-100之间,模拟
Random random = new Random();
int randomNumber = random.Next(10); // 生成一个介于 0 和 100(包括 0 和 100)之间的随机整数
//根据oid找到要素
IFeature f = featureClass.GetFeature(randomNumber);
//获取地图上的IFeatureSelection接口
IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
// 清除先前的选择
featureSelection.Clear();
IRgbColor color = new RgbColorClass();
color.Red = 255;
featureSelection.SelectionColor = color;
// 选择指定的要素
featureSelection.Add(f);
//定位的方法
// 获取要素的几何图形
IGeometry featureGeometry = f.ShapeCopy;
// 获取 IEnvelope 接口,用于获取要素范围
IEnvelope envelope = featureGeometry.Envelope;
// 将视图焦点设置为要素范围的中心点
this.axMapControl1.Extent = envelope;
// 获取地图的 IActiveView 接口并刷新视图
activeView.Refresh();
}`
`
private void button1_Click(object sender, EventArgs e)
{
IFeatureLayer featureLayer = (IFeatureLayer)this.axMapControl1.Map.get_Layer(0);
IFeatureClass featureClass = featureLayer.FeatureClass;
Random random = new Random();
int randomNumber = random.Next(10);
IFeature f = featureClass.GetFeature(randomNumber);
IGeometry featureGeometry = f.ShapeCopy
IEnvelope envelope = featureGeometry.Envelope;
this.axMapControl1.Extent = envelope;
ISelectionEnvironment selectionEnv = new SelectionEnvironment();
IRgbColor color = new RgbColorClass();
color.Red = 255;
selectionEnv.DefaultColor = color;
this.axMapControl1.Map.SelectByShape(featureGeometry, selectionEnv, false);
this.axMapControl1.Refresh(esriViewDrawPhase.esriViewForeground, null, null);
}
20231211_141056