Map Legend Help

1372
2
04-23-2010 04:09 AM
FlorCrowley
New Contributor
Hi all I know this is a long shot but if anyone has any ideas it would be greatly welcome. Below (& attached) is a sample code I got from the help section in ArcMaps it is used for adding a legend and North arrow to the map layout. I have attached a screen shot of the resultant map layout. However I would like to have the legend displayed without the file/layer name and field name. I have tried looking around on the internet and tried using the Ilegenditem interface and using the following code;

Dim pFeatureLayer As IFeatureLayer
Set pLegendItem = New HorizontalLegendItem
With pLegendItem
.ShowDescriptions = False
.ShowHeading = False
.ShowLabels = False
.ShowLayerName = False
End With

But no luck, if anyone has any ideas or could direct me where to get the macros for adding a map legend without the layer & field name in it or macros that could be easily altered so as to not display the layer or value names in the legend it would be greatly welcome.
Thanks





Public Sub AddMapSurrounds()

Dim pMxDoc As IMxDocument
Dim pActiveView As IActiveView
Dim pEnv As IEnvelope
Dim pID As New UID
Dim pMapSurround As IMapSurround
Dim pMarkerNorthArrow As IMarkerNorthArrow
Dim pCharacterMarkerSymbol As ICharacterMarkerSymbol
Dim pLegendItem As ILegendItem
Set pMxDoc = Application.Document
Set pActiveView = pMxDoc.PageLayout
Set pEnv = New Envelope
Dim pFeatureLayer As IFeatureLayer


'Add a north arrow
pEnv.PutCoords 0.2, 0.2, 1, 1
pID.Value = "esriCore.MarkerNorthArrow"
Set pMapSurround = CreateSurround(pID, pEnv, "North Arrow", pMxDoc.FocusMap, pMxDoc.PageLayout)
'Change out the default north arrow
Set pMarkerNorthArrow = pMapSurround 'QI
Set pCharacterMarkerSymbol = pMarkerNorthArrow.MarkerSymbol 'clones the symbol
pCharacterMarkerSymbol.CharacterIndex = 200 'change the symbol
pMarkerNorthArrow.MarkerSymbol = pCharacterMarkerSymbol 'set it back

'Add a legend
'In this case just use the default legend
pEnv.PutCoords 7.5, 0.2, 8.5, 4
pID.Value = "esriCore.Legend"

Set pFeatureLayer = pMxDoc.FocusMap.Layer(0)
Set pLegendItem = HorizontalLegendItem
'??LegendItem???????
With pLegendItem
Set .Layer = pFeatureLayer
'.Columns = 1
.ShowDescriptions = False
.ShowHeading = False
.ShowLabels = False
.ShowLayerName = False
End With

Set pMapSurround = CreateSurround(pID, pEnv, "Legend", pMxDoc.FocusMap, pMxDoc.PageLayout)


'Set pLegendItem =
'pLegendItem.ShowLayerName = False
'pLegendItem.ShowLabels = False
'Set .Layer = pFeatureLayer
'.Columns = 1
'.ShowDescriptions = False
'.ShowHeading = False


'End With


'Refresh the graphics
pActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
End Sub


Private Function CreateSurround(pID As UID, pEnv As IEnvelope, strName As String, _
pMap As IMap, pPageLayout As IPageLayout) As IMapSurround

Dim pGraphicsContainer As IGraphicsContainer
Dim pActiveView As IActiveView
Dim pMapSurroundFrame As IMapSurroundFrame
Dim pMapSurround As IMapSurround
Dim pMapFrame As IMapFrame
Dim pElement As IElement

'MapSurrounds are held in a MapSurroundFrame
'MapSurroundFrames are related to MapFrames
'MapFrames hold Maps
Set pGraphicsContainer = pPageLayout
Set pMapFrame = pGraphicsContainer.FindFrame(pMap)
Set pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pID, Nothing)
pMapSurroundFrame.MapSurround.Name = strName

'Set the geometry of the MapSurroundFrame to give it a location
'Activate it and add it to the PageLayout's graphics container
Set pElement = pMapSurroundFrame
Set pActiveView = pPageLayout
pElement.Geometry = pEnv
pElement.Activate pActiveView.ScreenDisplay
pGraphicsContainer.AddElement pElement, 0

Set CreateSurround = pMapSurroundFrame.MapSurround
End Function
0 Kudos
2 Replies
by Anonymous User
Not applicable
Original User: msteinbrenner

here is some code for a custom legend i wrote.

you won't need it all, but in order to turn off the layername and the fieldname you'll need most of it.

Public Sub AddLegend()
  Dim pDoc As IMxDocument
  Dim pPageLayout As IPageLayout
  Dim pMap As IMap
  Dim pID As New UID
  Dim pEnv As IEnvelope
  Dim pMapSurrFrame As IMapSurroundFrame
  Dim pMapSurround As IMapSurround
  Dim pElement As IElement
  Dim pLegend As ILegend
  Dim pLegendFormat As ILegendFormat
  Dim i As Long, lCount As Long
  Dim pLegendItem As ILegendItem
  Dim pLayer As ILayer
 
  'Get the Focus Map and Page Layout
  Set pDoc = ThisDocument
  Set pPageLayout = pDoc.PageLayout
  Set pMap = pDoc.FocusMap
 
  'Add a legend
  Set pEnv = New Envelope 'Create a geometry for the legend
        If strLayoutType = "Landscape" Then
            pEnv.PutCoords 0.31, 10.26, 2.31, 14.26
        Else
            pEnv.PutCoords 0.31, 16.1, 2.31, 20.1
        End If
 
  pID.value = "esriCore.Legend"
  Set pMapSurrFrame = CreateSurround(pID, pEnv, "Legend", pMap, pDoc)
  Set pElement = pMapSurrFrame
  Set pMapSurround = pMapSurrFrame.MapSurround
 
  'Modify the legend
  Set pLegend = pMapSurround
  With pLegend
    'Example of properties you can set with ILegend
    .AutoAdd = False
    .AutoVisibility = True
  End With
  Set pLegendFormat = pLegend.Format
  With pLegendFormat
    'Example of properties you can set with ILegendFormat
    .ShowTitle = False
    .DefaultPatchHeight = 9
    .DefaultPatchWidth = 18
    .TextGap = 0.1
    .HorizontalItemGap = 9
  End With
 
  'Reassign the legend format to the legend
  Set pLegend.Format = pLegendFormat
 
  'Assign Text properties for labels and heading
  Dim pTextSym As IFormattedTextSymbol
  Set pTextSym = New TextSymbol
  Dim pFontA As IFontDisp
  Set pFontA = New StdFont
  pFontA.Name = "Arial"
  pFontA.size = 10
  pFontA.Bold = True
  pTextSym.Font = pFontA
  pTextSym.HorizontalAlignment = esriTHALeft
  pTextSym.VerticalAlignment = esriTVABottom
 
  'Check individual legend items
  lCount = pLegend.ItemCount
  For i = lCount - 1 To 0 Step -1
    Set pLegendItem = pLegend.Item(i)
    Dim pLegendClassFormat As ILegendClassFormat
    Set pLegendClassFormat = pLegendItem.LegendClassFormat
    pLegendClassFormat.LabelSymbol = pTextSym
    With pLegendItem
        .ShowLayerName = False
        .ShowHeading = True
        .HeadingSymbol = pTextSym
        .Columns = legendcolumns
    End With
    'See if this is a layer that we want to show in the legend
    Set pLayer = pLegendItem.Layer
    If pLayer.Name <> "Left Side" Then
      pLegend.RemoveItem i 'Remove the item from the legend
    End If
  Next i
 
  'Refresh the legend
  Call RefreshLegend(pElement, pPageLayout, True)
End Sub

if your having problems let me know i can can probably show you exacly what you need out of this.

max
0 Kudos
FlorCrowley
New Contributor
Max Thanks very much for getting back to me but  unfortunately I am having trouble with the my  �??CreateSurround�?� routine that defines the page layout etc that I already had and making it compatible with the code you posted would you have the code where you defined �??CreateSurround�?�  . Here what I have so far
Again any help greatly welcome,


Private Function CreateSurround(pID As UID, pEnv As IEnvelope, strName As String, _
              pMap As IMap, pPageLayout As IPageLayout) As IMapSurround

  Dim pGraphicsContainer As IGraphicsContainer
  Dim pActiveView As IActiveView
  Dim pMapSurroundFrame As IMapSurroundFrame
  Dim pMapSurround As IMapSurround
  Dim pMapFrame As IMapFrame
  Dim pElement As IElement

  'MapSurrounds are held in a MapSurroundFrame
  'MapSurroundFrames are related to MapFrames
  'MapFrames hold Maps
  Set pGraphicsContainer = pPageLayout
  Set pMapFrame = pGraphicsContainer.FindFrame(pMap)
  Set pMapSurround = pMapFrame.CreateSurround(pID, Nothing)
  pMapSurround.MapSurround.Name = strName

  'Set the geometry of the MapSurroundFrame to give it a location
  'Activate it and add it to the PageLayout's graphics container
  Set pElement = pMapSurroundFrame
  Set pActiveView = pPageLayout
  pElement.Geometry = pEnv
  pElement.Activate pActiveView.ScreenDisplay
  pGraphicsContainer.AddElement pElement, 0

  Set CreateSurround = pMapSurroundFrame.MapSurround
End Function



Public Sub AddLegend()
Dim pMxDoc As IMxDocument
Dim pPageLayout As IPageLayout
Dim pMap As IMap
Dim pID As New UID
Dim pEnv As IEnvelope
Dim pMapSurroundFrame As IMapSurroundFrame
Dim pMapSurround As IMapSurround
Dim pElement As IElement
Dim pLegend As ILegend
Dim pLegendFormat As ILegendFormat
Dim i As Long, lCount As Long
Dim pLegendItem As ILegendItem
Dim pLayer As ILayer

'Get the Focus Map and Page Layout
Set pMxDoc = ThisDocument
Set pPageLayout = pMxDoc.PageLayout
Set pMap = pMxDoc.FocusMap

'Add a legend
Set pEnv = New Envelope 'Create a geometry for the legend
If strLayoutType = "Landscape" Then
pEnv.PutCoords 0.31, 10.26, 2.31, 14.26
Else
pEnv.PutCoords 0.31, 16.1, 2.31, 20.1
End If

pID.Value = "esriCore.Legend"
Set pMapSurroundFrame = CreateSurround(pID, pEnv, "Legend", pMap, pDoc)
Set pElement = pMapSurroundFrame
Set pMapSurround = MapSurroundFrame.MapSurround

'Modify the legend
Set pLegend = pMapSurround
With pLegend

�?��?�. Etc etc
0 Kudos