Does anyone know how to add map text programmatically?

2754
3
08-06-2015 11:48 AM
EllenBryson2
Occasional Contributor

The AGX version 2500 supports map text, either fixed to a specific location on earth's surface, or positioned relative to the map display (e.g, always top center, regardless of where user zooms.) However, the SDK was unchanged from version 1750. I'd like to create text programmatically in an add-in but don't see how to do that. To do this, I need to create something (graphic, text overlay, ?) but don't see any constructor for this type of text.

Thanks for any suggestions.

~Ellen

Tags (3)
0 Kudos
3 Replies
NormanDeschamps
New Contributor III

Hi Ellen,

Here is some code that I wrote to create a "dirty dot" (i.e. changed color as more changes were made to the map) that creates an overlay dot programmatically. It is also changing the size and color of the dot programatically based on the number of changes. Basically you are creating an imageoverlay graphic, then adding that ImageOverlay to the map's ForegroundOverlays collection.  These are all undocumented in the API; I found them by simply reading through the different routines.

//set up the characteristics of the dirty dot that won't change

ddotOverlay = new ImageOverlay("DirtyDot", ESRI.ArcGISExplorer.Mapping.Symbol.Marker.Sphere.Green.GetBitmap());

ddotOverlay.SizeMode = SizeMode.Absolute;

ddotOverlay.DisplayPosition = DisplayPosition.TopLeft;

//Actually creating the new dot and adding it to the overlay graphics collection

Symbol ddotSymbol = null;

ddotSymbol = ESRI.ArcGISExplorer.Mapping.Symbol.Marker.Sphere.Green;

int dotsize = 16 + (8 * ChangeCounter);

ddotOverlay.ChangeImage(ddotSymbol.GetBitmap(dotsize, dotsize));

ddotOverlay.Transparency = 100 - (12 * ChangeCounter); //Change level of transparancy of dot based on the number of changes           

ESRI.ArcGISExplorer.Mapping.MapDisplay _mapDisp = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay;

ImageOverlayOrderCollection OverlayCollection = _mapDisp.ForegroundOverlays;

if (OverlayCollection.Contains(ddotOverlay))

{

OverlayCollection.Remove(ddotOverlay);

}

OverlayCollection.AddToTop(ddotOverlay);

The process is much easier for adding non-overlay graphics (I.e. attached to a specific map coordinate). Simply create a variable pointing to the ActiveMapDisplay Graphics collection, then add any map graphics to that:

private GraphicCollection _graphics = null;

_graphics = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Graphics;

Graphic gPt = new Graphic([AGX point here]);

gPt.Symbol = Symbol.Marker.Sphere.Blue;

_graphics.Add(gPt);

I know that these are only for symbols, not text, but I know you can do it with similar processes. Sorry, gone for a few days now so I won't be able to respond till Wednesday. But if you are still stuck then, let me know and I will hunt that the specifics for adding text as well.

Norm

0 Kudos
EllenBryson2
Occasional Contributor

Norm,

Thanks very much. I was hoping you'd pipe in. I took your suggestions and your code and got it working!

For those interested, I've posted the code below. It basically creates an Image Overlay by creating a bitmap from given text, using your desired font, colors, etc and then adding that bitmap to the map as the overlay. Note the use of the Imports System.Drawing.Imaging, which is required.

Imports System.Drawing.Imaging

... (other coding as needed to achieve the goal)

Dim mapDisp As MapDisplay = ESRI.ARcGISExplorer.Application.Application.ActiveMapDisplay

Dim overlayColl As ImageOverlayOrderCollection = mapDisp.ForegroundOverlays

'create the text you want to appear. set colors, foreground, background, font size, etc.

Dim overlayText As String = "Here's the text I'd like to appear"

Dim BackColor As Color = Color.Orange

Dim FontColor As Color = Color.White

Dim borderColor As Color = Color.Black

Dim fontName As String = "Calibri"

Dim fontSize As Integer = 16

dim objFont As New Font (fontName, fontSize)

Dim height As Integer = fontSize * 2

Dim width As Integer = 400

Dim myPen As System.Drawing.Pen = New Pen(borderColor,2)

Dim objPoint As New PointF(5.0F,5.0F) 'this is not an esri point. it's a system drawing point

Dim objBrushForeColor As New SolidBrush(fontColor)

Dim objBrushBackColor As New SolidBrush(backColor)


'make a new bitmap at the desired size

dim objBitmap As New bitmap(width,height)

'make a graphic from this empty bitmap

Dim objGraphics As Graphics =  Graphics.FromImage(objBitmap)

'modify the graphic as desired/needed

objGraphics.DrawRectangle(myPen,0,0,width,height)

objGraphics.FillRectangle(objBrushBackColor,objPoint.x,objPoint.Y,width,height)

objDrawString(overlayText, ojbFont, objBrushForeColor,objPoint)

'now create the image overlay using this bitmap

Dim indOverlay As ImageOverlay = New ImageOverlay("Image Source",objBitmap)

indOverlay.DisplayPosition = DisplayPosition.TopCenter

mapDisp.Map.ImageOverlayDrawingOrder.AddToTop(indOverlay)

thanks again Norm

'editors note. changes were made throughout on 9/7/2015. several errors were found

~Ellen

0 Kudos
NormanDeschamps
New Contributor III

Glad I could help Ellen.

I've been looking at the ImageOverlay class, and there are a number of hidden properties for creating text directly within the class without having to create a bitmap of the text (or at least that is what it seems). There are hidden properties for font, font size, transparency, color, etc. However, I couldn't figure out how to create such overlays, since all of the methods available require a bitmap, as you have done. I'm sure it has something to do with how you can create overlay text for slides, but I would have to play more with it to figure out exactly how it works. Might be worth looking into if you run into trouble with having the text display cleanly.

Since you have done such an excellent job with your solution, you should mark your question as answered, so others can be assured that it is worth reading through this thread to solve this problem!

Norm

0 Kudos