如何以编程方式将活动图形图层添加到地图?

时间:2016-07-07 16:16:29

标签: arcgis esri arcmap arcobjects

我正在使用ArcObjects在C#中编写WPF应用程序。

我的表单上有一个ESRI.ArcGIS.Controls.AxMapControl,我试图在它上面绘制一些图形元素。

我正在开发的地图是佐治亚州客户提供的mdf。

我正在尝试一个我在这里找到的例子:How to interact with map elements

public void AddTextElement(IMap map, double x, double y)
{
    IGraphicsContainer graphicsContainer = map as IGraphicsContainer;
    IElement element = new TextElementClass();
    ITextElement textElement = element as ITextElement;

    //Create a point as the shape of the element.
    IPoint point = new PointClass();
    point.X = x;
    point.Y = y;
    element.Geometry = point;
    textElement.Text = "Hello World";
    graphicsContainer.AddElement(element, 0);

    //Flag the new text to invalidate.
    IActiveView activeView = map as IActiveView;
    activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

花了一些时间来弄清楚如何将亚特兰大的纬度/长度投影到地图的坐标系,但我很确定我已经做对了。根据我在地图上使用识别工具时看到的位置数据,传入AddTextElement()的x / y值显然位于亚特兰大地区。

但我没看到文字。一切似乎都正常,但我没有看到文字。

我可以看到许多可能性:

  • 图层我将TextElement添加到isn不可见或不存在。
  • 我需要将空间参考系统应用到我设置为TextElement几何的点
  • 文字很好,但字体有问题 - 它看起来很小,或者是透明的颜色等。

Haven找到了一条线索,

我希望有一些明显的东西我不知道。

===

我一直在继续玩这个游戏,因为我的原始帖子,我发现问题在于缩放 - 文本显示在它应该的位置,只有不可读的小。

这就是Rich Wawrzonek所建议的。

如果我设置一个具有指定大小的TextSymbol类,则大小确实适用,并且我看到文本变大或变小。不幸的是,当地图放大和缩小时,文本仍会调整大小,我尝试设置ScaleText = false并不能解决问题。

我的最新尝试:

public void AddTextElement(IMap map, double x, double y, string text)
{
    var textElement = new TextElementClass
    {
        Geometry = new PointClass() { X = x, Y = y },
        Text = text,
        ScaleText = false,
        Symbol = new TextSymbolClass {Size = 25000}
    };

    (map as IGraphicsContainer)?.AddElement(textElement, 0);

    (map as IActiveView)?.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

我认识到上述内容的组织方式与通常使用ESRI示例代码的方式截然不同。我发现ESRI的做法是每次难以阅读,但从一个切换到另一个是非常机械的。

这是以更传统的方式组织的相同功能。行为应该是相同的,我看到完全相同的行为 - 文本被绘制到指定的大小,但随着地图的缩放而缩放。

public void AddTextElement(IMap map, double x, double y, string text)
{
    IPoint point = new PointClass();
    point.X = x;
    point.Y = y;

    ITextSymbol textSymbol = new TextSymbolClass();
    textSymbol.Size = 25000;

    var textElement = new TextElementClass();
    textElement.Geometry = point;
    textElement.Text = text;
    textElement.ScaleText = false;
    textElement.Symbol = textSymbol;

    var iGraphicsContainer = map as IGraphicsContainer;
    Debug.Assert(iGraphicsContainer != null, "iGraphicsContainer != null");
    iGraphicsContainer.AddElement(textElement, 0);

    var iActiveView = (map as IActiveView);
    Debug.Assert(iActiveView != null, "iActiveView != null");
    iActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

有关为何忽略ScaleText的任何想法?

1 个答案:

答案 0 :(得分:1)

您只是设置文本元素的几何和文本。您还需要设置Symbol和ScaleText属性。 ScaleText属性boolean将确定它是否随地图缩放。需要通过ITextSymbol接口创建和设置Symbol属性。

有关Esri的例子,请参阅here