I'm writing a WPF applicating, in C#, using ArcObjects.
I have an ESRI.ArcGIS.Controls.AxMapControl on my form, and I'm trying to draw some graphics elements on top of it.
The map I'm developing with is a customer-provided mdf of the state of Georgia.
I'm trying an example I found here: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000001rv000000
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); }
It took while to figure out how to project the lat/long of Atlanta to the coordinate system of the map, but I'm pretty sure that I've got it right. The x/y values I'm passing into AddTextElement() are clearly within the Atlanta area, according to the Location data I see when I use the Identify tool on the map.
But I'm not seeing the text. Everything seems to be working correctly, but I'm not seeing the text.
I can see a number of possibilities:
- The layer I'm adding the TextElement to isn't visible, or doesn't exist.
- I need to apply a spatial reference system to the point I'm setting as the TextElement's geometry
- The text is drawing fine, but there's something wrong with the font - it's invisibly small, or in a transparent color, etc.
Haven't a clue, which.
I was hoping there was something obvious I was missing.
--- More info ---
I've been looking at a different tutorial:
http://resources.esri.com/help/9.3/arcgisengine/dotnet/7bd52ed1-18ae-4aa7-8cde-e9eaed9537fe.htm
This includes a custom control that writes the current date to the map, at the point that you click it with the mouse:
public override void OnMouseDown(int Button, int Shift, int X, int Y) { //Get the active view. IActiveView activeView = m_hookHelper.ActiveView; //Create a new text element. ITextElement textElement = new TextElementClass(); //Create a text symbol. ITextSymbol textSymbol = new TextSymbolClass(); textSymbol.Size = 25; //Set the text element properties. textElement.Symbol = textSymbol; textElement.Text = DateTime.Now.ToShortDateString(); //Query interface (QI) for IElement. IElement element = (IElement)textElement; //Create a point. IPoint point = new PointClass(); point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); //Set the element's geometry. element.Geometry = point; //Add the element to the graphics container. activeView.GraphicsContainer.AddElement(element, 0); //Refresh the graphics. activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
This actually succeeds in writing text to the map, but it confuses me.
When I click on the map, and enter into OnMouseDown, X is 183 and Y is 266 - clearly pixel coordinates. (That they are of type int would suggest that.)
The but after point is created, by calling DisplayTransformation.ToMapPoint(), it has X and Y of 2.5048801369863014 and 8.3338184931506856. That's not lat/long and it's not map coordinates. But when I use that as my TextElement's geometry and add it to the GraphicsContainer, it does display the text on the map.
Are these some sort of screen coordinates?
If so, how do I convert lat/long to screen coordinates?
And if they are screen coordinates, why doesn't my first example work, when I use the screen coordinate values to locate the text?
----
So, I added the custom control from the second example to the map on the first. Noticed two things:
1. The coordinates of the point returned by activeView.ScreenDisplay.DisplayTransformation.ToMapPoint() appear to be map coordinates.
2. I still don't see the text appear on the map.
This suggests to me that the problem isn't coordinates, or fonts, or colors, but that there isn't an active graphics layer on the map.
How do I add one?
----
At this point, I have to say "never mind".
Turns out my problem is scaling. My text is being drawn on the map, only at a scale that is invisible unless I zoom in considerably. My "Hello, world" text is drawn at a fixed size, on the map, and scales with the map. With a default size of only a hundred feet or so, which isn't visible when zoomed to see the entire state. If I set the size to 25, it's a bit larger, but still essentially invisible. I need to set it to 25000 before its legible on the map.
What I need, now, is to figure out how to draw markers and text on the map at a size that is independent of the zoom level. And that's another question, entirely.