Use Cases > PTV xMap > How to Use CustomLayer

How to Use CustomLayer

This sample is based on mapping with a BoundingBox . The task in this example is to draw a line into the map. Actually we draw an entry lane for the airport. Basically we use the method renderMapBoundingBox() and add a CustomLayer which we set as another additional argument.

The class CustomLayer includes Lines which is defined as an array of lines. This array contains an array of LineString, which again contains PlainLineString, and this object finally accepts a number of PlainPoint.

Basic mapping

Defining an Array of PlainPoint

First of all, the line we want to draw includes a set of points. These are the so called PlainPoint with their typical properties x- and y- coordinates. We have to specify an array of PlainPoint[ ] at first in order to draw a line into our map. In our example we add 4 points.

Step 1: Instantiate a new object plainPoints.

Java snippet
// Note: Third dimension added; necessary for java clients 1.17 and higher
PlainPoint[] plainPoints = new PlainPoint[5];
plainPoints[0] = new PlainPoint(681854.0, 6372751.0, null);
plainPoints[1] = new PlainPoint(680941.0, 6370524.0, null);
plainPoints[2] = new PlainPoint(683598.0, 6371416.0, null);
plainPoints[4] = new PlainPoint(679546.0, 6373244.0, null);

Defining a PlainLineString

After adding the points we create a new object PlainLineString wherein we are able to add new plainPoints. This is the first time our construct of single points is going to be a real line. Please notice that the class PlainLineString is not the only way to represent a line. There are three different formats to represent a line: lineString, wkb and wkt. This is why we need another intermediate step before we combine our lines. But keep in mind that it's possible to define different formats for lines and combine them altogether in a next step.

Step 2: Define a new object PlainLineString.

Java snippet
PlainLineString plainLineString = new PlainLineString(plainPoints);

Defining an Array of LineString

In this example we use the class LineString[] and include the format plainLineString with the corresponding method setLineString(). Please notice that the class LineString[] defines three different methods for the lines:

Detailed information is given in our technical documentation which can also be found at PTV's Developer Zone.

Step 3: Create a new object lineStrings.

Java snippet
LineString[] lineStrings = new LineString[1];
lineStrings[0] = new LineString();
lineStrings[0].setLineString(plainLineString);

Defining an array of Lines

We can define the characteristics of each line using the line options which contain the necessary properties such as color, width or visibility.

Step 4: Add the properties into the object options and set the line options into the object lines which both were instantiated before.

Please notice that each line has its own options.

Java snippet
Lines[] lines = new Lines[1];
LineOptions options =  new LineOptions()
LinePartOptions partoptions = new LinePartOptions();
partoptions.setVisible(true);
partoptions.setColor(new Color(255,50,20));
partoptions.setWidth(7);
options.setMainLine( partoptions );
lines[0] = new Lines(lineStrings, options);

Instantiating a CustomLayer Object

We have yet prepared all elements of our lines and we are ready to put our lines into a new layer customLayer. It is important to know that you have to specify the level where you want to place your layer. This can be specified with the method setdrawPriority() which is defined by its class CustomLayer.

Step 5: Define customLayer and set the necessary properties such as visibility, drawing priorities, lines and object information.

Java snippet
CustomLayer customLayer = new CustomLayer();
customLayer.setVisible(true);
customLayer.setDrawPriority(25000);
customLayer.setLines(lines);
customLayer.setObjectInfos(ObjectInfoType.NONE);

Create an Array of Layers with the CustomLayers

Last thing you have to do before calling the PTV xMap Server is to instantiate a new array Layer[ ] and fill it with the corresponding layers. Please notice that more than one layer can be defined. In this example we focus on one custom layer.

Step 6: Create an array of layers.

Java snippet
Layer[] layers = new Layer[]{ customLayer };

Calling

CustomLayer will be added as the fourth argument layers of the used method renderMapBoundingBox().

Step 7: Call renderMapBoundingBox. Note: all arguments are filled with the corresponding properties.

Java snippet
Map map = xmapClient.renderMapBoundingBox(boundingBox, mapParams, imageInfo,
layers, includeImageInResponse);

Source Code

The example drawing a simple line with CustomLayer is also executable as a stand-alone java program.