Measuring Distance

Switch the mouse mode to distance measurement mode, then measure the distance between clicked points. The measurement results are returned as events and visualized using POI and Line.

Interface Functionality

When you click on the point you want to measure, the distance value between that point and the previous one is displayed. Double-clicking ends the distance measurement. Clicking on another point after ending a measurement will display a new set of measurement results.

The result values display both the intermediate distance values and the cumulative distance values.

chevron-rightHere is the complete code that implements the above functionality.hashtag

Following are the detailed steps of the code.

Implementing the Functionality

Global Variables

Before implementing the functionality, declare variables that will be used globally.

These variables help manage the count of POI objects and measurement objects after ending a measurement.

step 1. Creating a Layer

A layer is created to visualize the distance measurement icons and values. Since the distance values will be displayed as POI, the layer type is set to ELT_3DPOINT.

For more information on layer types, please refer here.

step 2 - 1. Setting Callback Functions

Callback functions are registered to receive the calculated distance from the engine. Callback functions are registered through JSOption.

(For detailed API descriptions, see callBackAddPoint and callBackCompletePoint).

Two types of callback functions are registered:

When the mouse mode is set to MML_ANALYS

_DISTANCE_STRAIGHT, the specified callback function is called upon clicking the measurement points.

For setting the mouse mode, refer to step 3. Changing Mouse Mode.

step 2 - 2. Distance Measurement Callback Function Creation

This is the callback function executed on left mouse click. It visualizes the calculated distance.

step 2 - 3. Distance Measurement End Callback Function Creation

This is the callback function executed on double-click. It ends the distance measurement.

step 3. Changing Mouse Mode

The mouse mode is changed for distance measurement.

For more information on mouse modes, please refer here.

step 4 - 1. Creating Distance Icon

An icon is created to render the returned distance value. The icon image created in this step will be applied to the POI object that will display the measurement value.

The process of creating a POI object and applying the result image is described in step 5. Creating Distance Object.

There are two types of images created in this step:

Middle distance display icon image.

  • drawRoundRect function (step 4-3. Create distance rectangle icon) and setText([step 4-4. Create distance measurement result value icon](tutorial _distance.md#step-4-4.-icon)) Combine the functions to create a round text box image.

Cumulative distance display icon image

  • drawBalloon function (step 4-2. Create distance speech bubble icon) and setText([step 4-4. Create distance measurement result value icon](tutorial _distance.md#step-4-4.-icon)) Combine the functions to create a round text box image.

step 4 - 2. Creating Distance Balloon Icon

A balloon image is drawn to display the cumulative distance values.

step 4 - 3. Creating Distance Rectangle Icon

A rounded rectangle balloon image is drawn to display the intermediate distance values.

step 4 - 4. Creating Distance Measurement Result Value Icon

The calculated distance value is drawn as text on the drawn balloon.

step 4 - 5. Converting Distance Measurement Units (m/km)

The returned distance value is optionally converted to m/km text.

step 5. Creating Distance Object

A POI object is created using the icon and added to the layer.

The image is applied to the newly created POI object by storing the image drawn on the canvas in JSSymbol.

JSSymbol

XDWorld has a JSSymbol class for storing and managing textures.

The images created in step 4 are stored in JSSymbol and will be linked to the newly created POI object.

circle-info

You can also simply register image data without using JSSymbol.

This process is described in the Creating POI tutorial.

step 6. Resetting Distance Measurement

The distance measurement results and objects are reset.

Result Screen

After completing all the steps, the distance measurement functionality is completed, displaying distance information for clicked points.

If you want to see the live code for the distance measurement process, please click herearrow-up-right.

Last updated