Display the measurement of current segment in Measurement Widget?

3612
2
05-06-2015 12:55 PM
AnnCrystal
New Contributor II

In this ESRI measurement widget sample

https://developers.arcgis.com/javascript/jssamples/widget_measurement.html​

when you draw a line, it dynamically displays the segment length. And, when you measure subsequent segments lengths, it add it to the previous length.

My question is: How can I add another result which dynamically shows the current segment length?

Any thoughts?

Thanks Ann

Tags (1)
2 Replies
PLadd
by
Occasional Contributor III

Here's how I figured out how to keep track of current segment length (I couldn't find any measurement properties that contained this value).  By keeping track of the "previous" total measurement, I can subtract it from the new total measurement to get the current segment length.  Perhaps there's an easier more concise approach.  Here's what I did:

//first create "global" variables at top of js

var segmentLength = 0; //used in measure tool to track last segment length

var PrevM = 0; //used in measure tool to track last total measurement

var Mtype = ''; //used to track measurement units for display

//then create a span under your measureDiv for displaying segment text

<span id="spanSegment" class="spanStyle"></span>

//create measure tool

measurement = new esri.dijit.Measurement({

                map: map,

                defaultLengthUnit: esri.units.FEET,

                _defaultLocationUnit: "esriDegreeMinuteSeconds"

            }, dojo.byId("measurementDiv"));

            measurement.startup();

//when I start measuring, be sure to zero out any previous values

//this could also be done when a measure tool is clicked

            measurement.on("measure-start", function (evt) {

                if (evt.geometry.type == "polyline") {

                    segmentLength = 0;

                    PrevM = 0;

                    dojo.byId("spanSegment").innerHTML = 'Segment Length: ' + segmentLength.toFixed(1) + ' ' + Mtype;

                }

                else { dojo.byId("spanSegment").innerHTML = ''; }

            });

            // as each segment is measured, collect values and show segment length

            measurement.on("measure", function (evt) {

                if (evt.geometry.type == "polyline") {

                    var myVal = evt.values; //total measurement value

                    segmentLength = myVal - PrevM; //new segment value

                    PrevM = myVal; //remember current total measurement for next click

                    dojo.byId("spanSegment").innerHTML = 'Segment Length: ' + segmentLength.toFixed(1) + ' ' + Mtype;

                }

                else { dojo.byId("spanSegment").innerHTML = ''; }

            });

            //when done measuring, still need to show last segment, but then zero out values

            measurement.on("measure-end", function (evt) {

                if (evt.geometry.type == "polyline") {

                    var myVal = evt.values;

                    segmentLength = myVal - PrevM;

                    dojo.byId("spanSegment").innerHTML = 'Segment Length: ' + segmentLength.toFixed(1) + ' ' + Mtype;

                    segmentLength = 0;

                    PrevM = 0

                }

                else { dojo.byId("spanSegment").innerHTML = ''; }

            });

        

          //if user switches tools, say to area, then set segment text to empty to avoid confusion

          measurement.on("tool-change", function (evt) {

                dojo.byId("spanSegment").innerHTML = '';  //zero out current values

                Mtype = evt.unitName;

            });

         

          //track if user changes unit for display purposes

            measurement.on("unit-change", function (evt) {

                Mtype = evt.unitName;

            });

NOTE: I haven't figured out yet how to get perimeter values when measuring area.  I know you have to go to the root of the geometryServer values somehow but I'm struggling . . . .

0 Kudos
TimWitt2
MVP Alum

Hey Ann,

check out my draw tool, which includes what you need. Advanced Draw

Just check "Add Measurement" and you will see the total length and segment length.

Tim

0 Kudos