Code
//获得属性值
string ManagerId = dgvK001.Rows[e.RowIndex].Cells[0].Value.ToString();
string RoadId = dgvK001.Rows[e.RowIndex].Cells[2].Value.ToString();
if (RoadId.IndexOf("S") != -1 || RoadId.IndexOf("G") != -1)
RoadId = RoadId.Substring(0, 4);
string StartStake = dgvK001.Rows[e.RowIndex].Cells[5].Value.ToString();
//遍历整个地图
for (int i = 0; i < axMapControl.LayerCount; i++)
{
IFeatureLayer pLayer = null;
IFeatureClass iFeatureClass = null;
//获得图层
pLayer = (IFeatureLayer)axMapControl.get_Layer(i);
iFeatureClass = pLayer.FeatureClass;
//定义AE游标
IFeatureCursor pCursor;
//构建查询条件
IQueryFilter pFilter = new QueryFilterClass();
//多字段查询
pFilter.WhereClause = " LXBM = '" + RoadId.Trim() + "'" + " AND DWDM = '" + ManagerId.Trim() + "'" + " AND QDZH=" + StartStake.Trim() + "";
try
{
pCursor = iFeatureClass.Search(pFilter, false);
IFeature pFeature;
//获得查询的元素
pFeature = pCursor.NextFeature();
if (pFeature != null)
{
//要素的定义,如果是点直接定位
if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
{
this.axMapControl.CenterAt((IPoint)pFeature.Shape);
}
else
{
//获得定位的范围
IEnvelope pEnv = pFeature.Shape.Envelope;
pEnv.Expand(5, 5, true);
axMapControl.ActiveView.Extent = pEnv;
}
axMapControl.ActiveView.Refresh();
axMapControl.ActiveView.ScreenDisplay.UpdateWindow();
//用于解决先定位后闪烁的问题
//自定义闪烁功能
switch (pFeature.Shape.GeometryType)
{
case esriGeometryType.esriGeometryPoint:
FlashFeature.FlashPoint(axMapControl, axMapControl.ActiveView.ScreenDisplay, pFeature.Shape);
break;
case esriGeometryType.esriGeometryPolyline:
FlashFeature.FlashLine(axMapControl, axMapControl.ActiveView.ScreenDisplay, pFeature.Shape);
break;
case esriGeometryType.esriGeometryPolygon:
FlashFeature.FlashPolygon(axMapControl, axMapControl.ActiveView.ScreenDisplay, pFeature.Shape);
break;
default:
break;
}
axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
break;
}
}
catch { }
}
//获得属性值
string ManagerId = dgvK001.Rows[e.RowIndex].Cells[0].Value.ToString();
string RoadId = dgvK001.Rows[e.RowIndex].Cells[2].Value.ToString();
if (RoadId.IndexOf("S") != -1 || RoadId.IndexOf("G") != -1)
RoadId = RoadId.Substring(0, 4);
string StartStake = dgvK001.Rows[e.RowIndex].Cells[5].Value.ToString();
//遍历整个地图
for (int i = 0; i < axMapControl.LayerCount; i++)
{
IFeatureLayer pLayer = null;
IFeatureClass iFeatureClass = null;
//获得图层
pLayer = (IFeatureLayer)axMapControl.get_Layer(i);
iFeatureClass = pLayer.FeatureClass;
//定义AE游标
IFeatureCursor pCursor;
//构建查询条件
IQueryFilter pFilter = new QueryFilterClass();
//多字段查询
pFilter.WhereClause = " LXBM = '" + RoadId.Trim() + "'" + " AND DWDM = '" + ManagerId.Trim() + "'" + " AND QDZH=" + StartStake.Trim() + "";
try
{
pCursor = iFeatureClass.Search(pFilter, false);
IFeature pFeature;
//获得查询的元素
pFeature = pCursor.NextFeature();
if (pFeature != null)
{
//要素的定义,如果是点直接定位
if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
{
this.axMapControl.CenterAt((IPoint)pFeature.Shape);
}
else
{
//获得定位的范围
IEnvelope pEnv = pFeature.Shape.Envelope;
pEnv.Expand(5, 5, true);
axMapControl.ActiveView.Extent = pEnv;
}
axMapControl.ActiveView.Refresh();
axMapControl.ActiveView.ScreenDisplay.UpdateWindow();
//用于解决先定位后闪烁的问题
//自定义闪烁功能
switch (pFeature.Shape.GeometryType)
{
case esriGeometryType.esriGeometryPoint:
FlashFeature.FlashPoint(axMapControl, axMapControl.ActiveView.ScreenDisplay, pFeature.Shape);
break;
case esriGeometryType.esriGeometryPolyline:
FlashFeature.FlashLine(axMapControl, axMapControl.ActiveView.ScreenDisplay, pFeature.Shape);
break;
case esriGeometryType.esriGeometryPolygon:
FlashFeature.FlashPolygon(axMapControl, axMapControl.ActiveView.ScreenDisplay, pFeature.Shape);
break;
default:
break;
}
axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
break;
}
}
catch { }
}
这边要注意的是你需要定位的图层必须要有你需要查询的属性,在遍历整个地图的话则比较难统一字段,所以在获得元素的地方需要Catch掉容错.
图层也可以直接定义,假如你知道的话.