Use Cases > PTV xMap > How to Add Custom Text to a Map

How to Add Custom Text to a Map

This example shows how to create and style text objects, and how to position them on a map. Text objects can be used to add annotations to maps and points. In combination with vector shapes you may use them to mark and label areas or add descriptive texts to shapes or points. Below, we show you how text objects are initialized and what options you have to alter the appearance of the text.

Creating Text objects

A Text object consists of a string and a position at which the text should appear on the map.

Java snippet

Define point1 to set the text position. Then create a new object text1. Set your text as a string.

Point point1 = new Point();
 point1.setWkt("POINT(681254.0 6378399.0)");

 Text text1 = new Text();
 text1.setPosition(point1);
 text1.setText("this is your text");

Setting the text options

The text options allow you to set

Java snippet

Instantiate a class called textOptions and set the required properties such as name, size, font type, color, etc.

TextOptions textOptions = new TextOptions();
// font
Font font = new Font();
font.setName("Arial");
font.setSize(24);
font.setBold(true);
textOptions.setFont(font);

// font color
textOptions.setTextColor(new Color(0,0,0));

// background
textOptions.setFillBg(true);
textOptions.setBgColor( new Color(255,255,255) );

// frame
textOptions.setShowFrame(true);
textOptions.setFrameColor( new Color(0,0,255) );

// text align
textOptions.setAlignment(TextAlignment.CENTER);

// pixel offset of text
textOptions.setPixelX(0);
textOptions.setPixelY(0);

// offset of text relative to text's height
textOptions.setRelY(0);
textOptions.setRelY(0);

Applying the text options

To apply the text options to the text you have to group the class Text with the object TextOptions into another class so called Texts. If you want to apply a different style to each text object you have to create multiple Texts objects and assign text options individually for each of them.

Java snippet

Build a new object texts. Set options and the required phrases.

Texts texts = new Texts();
 texts.setOptions(textOptions)
 texts.setTexts(new Text[]{text1, text2});

Include Texts in a CustomLayer

Now we put the texts into a CustomLayer which can be used as shown in other examples.

Java snippet

Create a layer and set the required properties. Don't forget to include the already defined object texts using the new instantiated array Texts[ ].

CustomLayer customLayer = new CustomLayer()
 customLayer.setDrawPriority(25000);
 customLayer.setName("Text layer");
 customLayer.setObjectInfos(ObjectInfoType.REFERENCEPOINT);
 customLayer.setVisible(true);
 customLayer.setTexts(new Texts[]{ texts });

Source Code

The example mapping with text to map is also executable as a stand-alone java program.