5 votos

¿Cómo crear nueva leyenda utilizando ArcObjects?

Estoy tratando de agregar simple leyenda al mapa.

Escribí algunos código de prueba simple para ver lo que sucede pero no funcionó.

var map = activeView.FocusMap;

            IGraphicsContainer graphicsContainer = activeView.GraphicsContainer;
            IMapFrame mapFrame = graphicsContainer.FindFrame(map) as IMapFrame;
            UID uid = new UID();
            uid.Value = "esriCarto.Legend";
            IMapSurround mapSurround = map.CreateMapSurround(uid, null);
mapFrame.CreateSurroundFrame(uid, null);

            var legend = (ILegend)mapSurround;

            var legendItem = new VerticalLegendItemClass();
            legendItem.HeadingSymbol.Text = "served";
            legend.AddItem(legendItem);

            var legendItema = new VerticalLegendItemClass();
            legendItema.HeadingSymbol.Text = "unserved";
            legend.AddItem(legendItema);


            IElement element = legend as IElement;
            element.Geometry = envelope as IGeometry;
            graphicsContainer.AddElement(element, 0);

            activeView.Refresh();

La leyenda que busco debe ser similar a esto:

legend

¿Alguien tiene alguna enlace o Consejo que podría ayudarme a conseguirlo, por favor?

1voto

Nathan Bedford Puntos 3157

El siguiente código (que es básicamente una muestra adaptada desde el SDK) trabaja para mí, pero sólo si hay cualquier capas en el mapa. Aviso esto está en consonancia con la interfaz de usuario de ArcMap, que no permite insertar la leyenda cuando no capas están en el mapa.

private void AddLegendForMap(IMap map, IPageLayout pageLayout, double xInPageUnits, double yInPageUnits, double widthInPageUnits)
{
    if (map == null) throw new ArgumentNullException("map");
    if (pageLayout == null) throw new ArgumentNullException("pageLayout");

    var layoutGraphicsContainer = (IGraphicsContainer)pageLayout;
    var mapFrame = layoutGraphicsContainer.FindFrame(map) as IMapFrame;

    if (mapFrame == null)
    {
        throw new InvalidOperationException("Map frame for given map was not found on the page layout.");
    }

    var legendUid = new UIDClass {Value = "esriCarto.Legend"};

    var legendMapSurroundFrame = mapFrame.CreateSurroundFrame(legendUid, null);
    var legendMapSurround = legendMapSurroundFrame.MapSurround;

    double widthInPoints = 0;
    double heightInPoints = 0;

    ((IQuerySize)legendMapSurround).QuerySize(ref widthInPoints, ref heightInPoints);
    var widthToHeightRatio = widthInPoints / heightInPoints;

    var heightInPageUnits = widthInPageUnits * widthToHeightRatio;

    var envelope = new EnvelopeClass();
    envelope.PutCoords(xInPageUnits, yInPageUnits, xInPageUnits + widthInPageUnits, yInPageUnits + heightInPageUnits);

    ((IElement)legendMapSurroundFrame).Geometry = envelope;

    layoutGraphicsContainer.AddElement((IElement)legendMapSurroundFrame, 0);

    ((IActiveView)pageLayout).PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

1voto

alxcpa01101 Puntos 1

Se ha observado en los comentarios de que su programa se bloquea cuando lanzas a UID así:

mapFrame.CreateSurroundFrame((UID)uid, null); 

El problema podría ser que usted está lanzando a un C# Sistema UID como este. Para rectificar esto, usted deseará especificar explícitamente que desea convertir a la ArcObjects UID como en el fragmento de @PetrKrebs vinculados.

Trate de sustituir en el código anterior, para esto:

ESRI.ArcGIS.Carto.IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame((ESRI.ArcGIS.esriSystem.UID)uid, null); // Explicit Cast

A pesar de que se requiere más escribir, siempre es más seguro que claramente especifique la vía de sus referencias (en este caso es de ESRI.ArcGIS.esriSystem.UID o Sistema.Windows.UIElement.UID) cuando se hace referencia a nada en la programación, ya que evita ambigüedades tales como estos.

El compilador a veces coger una referencia ambigua, pero no siempre. Normalmente, por defecto en el Sistema de referencia si hay una referencia ambigua y sin advertencia/error se produce.

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X