Distance scale indicator

@Russell_Grew or anyone else,

I’m looking to implement a distance scale indicator. Does anyone have sample code or something in the Showcase to do this?

I would like to implement exactly what this site has https://nationalmap.gov.au/


But I did not see where this was implemented when I looked at the provided repository. https://github.com/TerriaJS/TerriaMap/

I think the easiest way for you might be to use TerriaJS itself as a library, they have a getting started guide here: https://github.com/TerriaJS/TerriaJS#getting-started

Otherwise you’ll need to find the source code for this in TerriaJS, extract it, and then implement it in your CesiumJS app.

I believe you can now find the code here

There was another thread about this(5 years ago)
2d scale implementation for Legend?

@Hyper_Sonic Yes, I’ve seen the previous threads. The moderator asked me to start my own thread since the others had been several years ago.

I’ve also seen that DistanceLegend.jsx file before. Do you know how to implement this without having to use the Terria library? I was hoping someone had something set up in the showcase.

I believe the gist of it is here

This shoots out 2 rays 1 pixel apart in the screen space. It then determines where they land on Earth, and determines the distance between those 2 landing spots, can call this distance pixelDistance. This lets you know how big a 2D screen pixel is when laid over the 3D view. Say pixelDistance is 10 meters, then a bar 10 pixels long would be a scale of 100 meters.

If the camera is very high though, having one scale bar would not be sufficient, due to the curvature of the Earth (1 pixel on the screen edge would represent a longer distance than on the screen center.)

1 Like

I started following the getting started guide on TerriaJS. My npm install command failed because it could not run the post install script.

I tried running using GITBash and Cygwin but failed:
WARN react-datepicker@0.53.0 requires a peer of react@^0.14.0 || ^15.0.0 but none is installed. You must install peer dependencies yourself.
npm ERR! errno 1
npm ERR! terriajs@7.11.6 postinstall: gulp post-npm-install
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the terriajs@7.11.6 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Not sure how to proceed. Now it seems I’m jumping down a rabbit hole that I was worried about when I first was asking this question on here.

https://docs.terria.io/guide/getting-started/ talks about the need to use Git Bash if you are running windows.

I think also that this may not be best the place for detailed Terria discussions.

Maybe I’m going about this the wrong way. Let me start simple, that way I can understand the pieces and parts a little at a time.

In the picture, is that box an infoBox? If so, how do you show whatever widget this is at the bottom right of the screen?

That’s just an HTML element added on the page on top of the CesiumJS canvas. So you’d need to create this HTML element, position it to the bottom (or top right etc) with CSS, and then update its contents whenever the CesiumJS camera changes.

At the bottom

barWidth determines width of barStyle. That box is a div, which contains a label and another div. That other div displays the bar using className and style properties. Not sure, but that probably translates to
if not SVG graphics. Progress bars can be set by pixels or % for either width or height.

For anyone else interested, I have a simple solution without having to use terriaJS. I did use some of the calculations from the provided file above DistanceLegend.jsx.

html file:

<!DOCTYPE html>
<html lang="en">
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

        padding: 10px 10px;
        bottom: 35px;
        right: 10px;
        position: absolute;
        display: inline-block;
        background: #304458;
        color: #bacdcc;
        border-radius: 5px;
        font-family: Arial;
        font-size: 14px;
        font-style: normal;
        text-align: center;
        display: inline-block;
        position: relative;
        background: #304458;
        color: #bacdcc;
        border-radius: 2px;
        padding: 0px 0px;
        margin: 0px 0px;
        font-family: Arial;
        font-size: 14px;
        font-style: normal;
        text-align: center;
        padding-left: 5%
        margin-top: 10px;
        margin-left: auto;
        margin-right: auto;

  <div id="scaleBox">
    <div id="scalebar" class="w3-container w3-grey scalebar"></div>
    <p id="scalebartag" class="scale-text">Distance Scale</p>
  <script src="./myScript.js"></script>

js myScript file:

//Zoom listener
viewer.camera.moveEnd.addEventListener(function() { 
   // the camera stopped moving

function updateDistanceScale()
    var geodesic = new Cesium.EllipsoidGeodesic();
    var distances = [
        1, 2, 3, 5,
        10, 20, 30, 50,
        100, 200, 300, 500,
        1000, 2000, 3000, 5000,
        10000, 20000, 30000, 50000,
        100000, 200000, 300000, 500000,
        1000000, 2000000, 3000000, 5000000,
        10000000, 20000000, 30000000, 50000000];

     // Find the distance between two pixels at the bottom center of the screen.
     var width = viewer.scene.canvas.clientWidth;
     var height = viewer.scene.canvas.clientHeight;

     var left = viewer.scene.camera.getPickRay(new Cesium.Cartesian2((width / 2) | 0, height - 1));
     var right = viewer.scene.camera.getPickRay(new Cesium.Cartesian2(1 + (width / 2) | 0, height - 1));
     var globe = viewer.scene.globe;
     var leftPosition = globe.pick(left, viewer.scene);
     var rightPosition = globe.pick(right, viewer.scene);

     if (typeof leftPosition == "undefined" || typeof rightPosition == "undefined") {
    var leftCartographic = globe.ellipsoid.cartesianToCartographic(leftPosition);
    var rightCartographic = globe.ellipsoid.cartesianToCartographic(rightPosition);

    geodesic.setEndPoints(leftCartographic, rightCartographic);
    var pixelDistance = geodesic.surfaceDistance * 3.28084; //meters to feet
    var pixelDistanceMiles = pixelDistance / 5280;

    // Find the first distance that makes the scale bar less than 100 pixels.
    var maxBarWidth = 100;
    var distance;
    var label;
    var units;

    for (var i = distances.length - 1; i >= 0; --i) {
         if (distances[i] / pixelDistance < maxBarWidth) {
            if(distances[i] > 5280)
                for (var j = distances.length - 1; j >= 0; --j)
                    if (distances[j] / pixelDistanceMiles < maxBarWidth)
                        distance = distances[j];
                        units = " mi";
                distance = distances[i];
                units = " ft";

    if (typeof distance !== "undefined") {

        label = distance.toString() + units;

        if(units === " mi")
            document.getElementById("scalebar").style.width = ((distance / pixelDistanceMiles) | 0).toString() + "px";
            document.getElementById("scalebar").style.width = ((distance / pixelDistance) | 0).toString() + "px";
    } else {
        document.getElementById("scalebar").style.width = "100px";
1 Like

Glad you got it working. So instead of a progress bar you just change the size of the div itself? Apparently the bitwise OR is used as a Math.floor shortcut.

An alternative to writing out the long array

var i=100000000;var j=1;
function next()
    if(j==4){i*=3/5;j=3;return i;}
    if(j==3){i*=2/3;j=2;return i;}
    if(j==2){i*=1/2;j=1;return i;}
    if(j==1){i*=1/2;j=4;return i;}