Select to view content in your preferred language

Add a labeled graphic to the map

700
1
07-10-2012 12:32 AM
MarcelMartinez
New Contributor
I'm attempting to create a button that will allow a user to add a labeled (coordinates) point (graphic) to a map. I've taken bits and pieces and brought them together. I can plot a point now but when the script gets to the point of labeling it crashes. Below is my code, please let me know what I have mixed up and perhaps what concept am I missing here.

    Protected Overrides Sub OnMouseDown(arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
        MyBase.OnMouseDown(arg)

        ' Get graphics container
        Dim pMXDocument As IMxDocument
        pMXDocument = My.ArcMap.Document
        Dim pActiveView As IActiveView
        pActiveView = pMXDocument.ActiveView
        Dim pGraphicsContainer As IGraphicsContainer
        pGraphicsContainer = pActiveView.GraphicsContainer

        ' Create green colour
        Dim pRGB As IRgbColor
        pRGB = New RgbColor
        With pRGB
            .Red = 84
            .Green = 255
            .Blue = 255
        End With


        'Current location of mouse point in map units
        Dim pPoint As IPoint
        pPoint = New ESRI.ArcGIS.Geometry.Point
        pPoint = My.ArcMap.Document.CurrentLocation

        ' Create point element

        Dim pPointEl As IMarkerElement = New MarkerElement

        'Create text element
        Dim ptextElement As ITextElement = New TextElementClass()

        Dim x = My.ArcMap.Document.CurrentLocation.X
        Dim y = My.ArcMap.Document.CurrentLocation.Y

        ptextElement.Text = x & ", " & y

        ' Create Element and set geometry
        Dim pElement As IElement = pPointEl
        pElement.Geometry = pPoint
        'pElement = ptextElement

        ' Create Point symbol (thick green line)
        Dim pPointSymbol As ISimpleMarkerSymbol
        pPointSymbol = New SimpleMarkerSymbol

        With pPointSymbol
            .Color = pRGB
            .Size = 10
        End With


        'Set font
        Dim pFormattedTextSymbol As IFormattedTextSymbol = New TextSymbolClass()


        With pFormattedTextSymbol
            .HorizontalAlignment = GetHorizontalAlignment()
            .VerticalAlignment = GetVerticalAlignment()
            With .Font
                .Name = "Times New Roman"
                .Size = 23
                .Bold = True
            End With
        End With

        ptextElement.Symbol = pFormattedTextSymbol
        pPointEl.Symbol = pPointSymbol


        ' Add to maps graphic container and refresh screen
        pGraphicsContainer.AddElement(pElement, 0)
        pGraphicsContainer.AddElement(ptextElement, 0)
        pGraphicsContainer = My.ArcMap.Document.FocusMap

        pElement.Activate(My.ArcMap.Document.ActiveView.ScreenDisplay)
        My.ArcMap.Document.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)

    End Sub
0 Kudos
1 Reply
PhilBlondin
Deactivated User
the below sub will label a point for you.   I also gave the labels a name in the element properties in case i wanted to delete them later through code i could find them by name.   


Public Sub ZoomToXY(ByVal pXYPoint As IPoint, ByVal pmap As IMap, ByVal pMx As IMxDocument)
        Try

            If pmap.SpatialReference Is Nothing Then
                MsgBox("No data frame spatial reference detected." & vbNewLine _
                & "This needs to be set first.", vbInformation)
                Exit Sub
            End If

            Dim pCurrentSpatialRef As ISpatialReference
            Dim pElement As IElement
            Dim pMarkerElement As IMarkerElement
            Dim pSimpleMarkerSymbol As ISimpleMarkerSymbol
            Dim pEnv As IEnvelope
            Dim pGC As IGraphicsContainer
            Dim pTextElement As ITextElement
            Dim pPointElemProp As IElementProperties
            Dim pLabelElemProp As IElementProperties

            pCurrentSpatialRef = pmap.SpatialReference

            'create a new point based on the input WGS84 mouse clicked point and project
            'to the map coordinate system
            Dim pPoint As Point = New Point
            pPoint.X = pXYPoint.X
            pPoint.Y = pXYPoint.Y
            pPoint.SpatialReference = pXYPoint.SpatialReference
            pPoint.Project(pmap.SpatialReference)

            Dim pColor As IRgbColor
            pColor = New RgbColor
            pColor.RGB = RGB(255, 0, 0)

            pElement = New MarkerElement
            pElement.Geometry = pPoint

            pPointElemProp = pElement
            pPointElemProp.Name = "XY Point"

            'make the red cross for the xy point
            pMarkerElement = pElement
            pMarkerElement.Symbol = New SimpleMarkerSymbol
            pSimpleMarkerSymbol = New SimpleMarkerSymbol

            pSimpleMarkerSymbol.Color = pColor
            pSimpleMarkerSymbol.Size = 8
            pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCross

            pMarkerElement.Symbol = pSimpleMarkerSymbol

            'zoom to to the red xy cross
            pEnv = pMx.ActiveView.Extent
            pEnv.CenterAt(pElement.Geometry)

            pMx.ActiveView.Extent = pEnv
            pmap.MapScale = pmap.MapScale

            'add the xy point element
            pGC = pMx.ActiveView
            pGC.AddElement(pMarkerElement, 0)

            pTextElement = New TextElement

            'create the lat long text element
            Dim pXYElement As IElement
            pXYElement = pTextElement 'QI
            pLabelElemProp = pXYElement
            pLabelElemProp.Name = "XY Label"
            pTextElement.Text = "Lat: " & Format(pXYPoint.X, ".0000000") & vbNewLine & "Long: " & Format(pXYPoint.Y, ".0000000")

            'set the geometry to the projected point
            pXYElement.Geometry = pPoint

            'add it to the map as well
            pGC.AddElement(pXYElement, 0)

            pMx.ActiveView.Refresh()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
0 Kudos