【问题标题】:How to programmatically add an active graphics layer to a map?如何以编程方式将活动图形图层添加到地图?
【发布时间】:2016-11-10 01:42:01
【问题描述】:

我正在使用 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 的图层不可见或不存在。
  • 我需要将空间参考系统应用于我设置为 TextElement 几何的点
  • 文字画得很好,但字体有问题 - 它太小了,或者是透明色等。

不知道是哪个。

我希望我缺少一些明显的东西。

===

自从我最初发布以来,我一直在玩这个,我发现问题在于缩放 - 文本显示在它应该出现的地方,只是小得难以阅读。

这是 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 被忽略的任何想法?

【问题讨论】:

标签: arcgis esri arcmap arcobjects


【解决方案1】:

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

有关 Esri 的示例,请参阅 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-16
    • 2012-01-15
    • 1970-01-01
    • 2023-03-19
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多