Monthly Archives: May 2010

ESRI Snippet: Adding a graphic to a map

Really handy sometimes for debugging to make sure you are dealing with the correct geometry:

Public Sub AddGraphicToMap(ByVal map As IMap, ByVal geometry As IGeometry)

 Dim graphicsContainer As IGraphicsContainer
 Set graphicsContainer = map

 Dim element As IElement
 Set element = Nothing

 Dim rgbColor As IRgbColor
 Set rgbColor = New rgbColor
 With rgbColor
 .Red = 111
 .Green = 111
 .Blue = 111
 .UseWindowsDithering = True
 End With

 Dim simpleFillSymbol As ISimpleFillSymbol
 Set simpleFillSymbol = New simpleFillSymbol

 simpleFillSymbol.Color = rgbColor
 simpleFillSymbol.Style = esriSFSForwardDiagonal

 Dim fillShapeElement As IFillShapeElement
 Set fillShapeElement = New PolygonElement

 fillShapeElement.Symbol = simpleFillSymbol
 Set element = fillShapeElement

 If Not (element Is Nothing) Then
 element.geometry = geometry
 graphicsContainer.AddElement element, 0
 End If

End Sub