首先点击获得Feature
代码:
Public Shared Sub SelectMouseDown(ByVal pixSize As Integer, ByVal x As Long, ByVal y As Long, ByVal pLayer As IFeatureLayer, ByVal pMap As IMap, ByVal justOne As Boolean)
Dim pRefreshArea As IInvalidArea
pRefreshArea = New InvalidArea
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
Dim pSpatialFilter As ISpatialFilter
Dim pFilter As IQueryFilter
Dim pActiveView As IActiveView
Dim pGeometry As IGeometry
Dim pPoint As IPoint
'将所有选择集合加入到pInvalidArea中,刷新时能清除原有的选择集合
Dim pOrigEnumFeature As IEnumFeature
Dim pOrigFeature As IFeature
pOrigEnumFeature = pMap.FeatureSelection
pOrigFeature = pOrigEnumFeature.Next
While pOrigFeature IsNot Nothing
pRefreshArea.Add(pOrigFeature)
pOrigFeature = pOrigEnumFeature.Next
End While
'初始话pLayer层的选择SelectionSet
Dim pFeatureSelect As IFeatureSelection
Dim pSelectionSet As ISelectionSet
pFeatureSelect = pLayer
pFeatureSelect.Clear()
pSelectionSet = pFeatureSelect.SelectionSet
Try
pMap.ClearSelection() '首先清除以前选择项
If pLayer Is Nothing Then Exit Sub
If Not TypeOf pLayer Is IGeoFeatureLayer Then Exit Sub
' Get the feature layer and class of the current layer
pFeatureLayer = pLayer
pFeatureClass = pFeatureLayer.FeatureClass
If pFeatureClass Is Nothing Then Exit Sub
' Get the mouse down position in map coordinates
pActiveView = pMap
pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)
pGeometry = pPoint
' Use a four pixel buffer around the cursor for feature search
Dim length As Double
length = ConvertPixelsToMapUnits(pMap, pixSize)
Dim pTopo As ITopologicalOperator
pTopo = pGeometry
Dim pBuffer As IGeometry
pBuffer = pTopo.Buffer(length)
pGeometry = pBuffer.Envelope
' Set up a Filter specific to this layer
pSpatialFilter = New SpatialFilter
pSpatialFilter.Geometry = pGeometry
Select Case pFeatureClass.ShapeType
Case esriGeometryPoint
pSpatialFilter.SpatialRel = esriSpatialRelIntersects
Case esriGeometryPolyline
pSpatialFilter.SpatialRel = esriSpatialRelIntersects
Case esriGeometryPolygon
pSpatialFilter.SpatialRel = esriSpatialRelIntersects
End Select
pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName
pFilter = pSpatialFilter
' Do the search
Dim pCursor As IFeatureCursor
pCursor = pFeatureLayer.Search(pFilter, True)
' and select the features on the map
Dim pFeature As IFeature
pFeature = pCursor.NextFeature
If pFeature IsNot Nothing Then
'如果justOne为真只添加一个
If justOne Then
pMap.SelectFeature(pLayer, pFeature)
pRefreshArea.Add(pFeature)
pSelectionSet.Add(pFeature.OID)
Else
While Not pFeature Is Nothing
pMap.SelectFeature(pLayer, pFeature)
pRefreshArea.Add(pFeature)
pSelectionSet.Add(pFeature.OID)
pFeature = pCursor.NextFeature
End While
End If
End If
'更新Map中的选择集合,并触发selectionChanged事件
pMap.ClearSelection()
pFeatureSelect.SelectionSet = pSelectionSet
pFeatureSelect.SelectionChanged()
'刷新屏幕
pRefreshArea.Display = pActiveView.ScreenDisplay
pRefreshArea.Invalidate(esriAllScreenCaches)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
由于是GDB,可以通过Shape_Length、Shape_Area获得长度、面积等信息。