Capture each segment Measrurement Tool

3317
8
Jump to solution
02-09-2016 12:38 PM
JohnPreston
Occasional Contributor

Hi, I saw a few older posts (three years old most recent) but I would like to capture length on each segment while using Measurement tool. I thought using "measure" event would work but that doesn't seem to fire. Can you help?

Here is how I am starting and capturing "measure" event...

var measurement;              

$('#liMeasurementOn').click(function () {                  

var divMeasure = ""                  

$('#titlePane').append(divMeasure);                  

$('#titlePane').show();                  

$('#liMeasurementOn').hide();                  

$('#liMeasurementOff').show();                  

dojo.disconnect(connects);                  

measurement = new Measurement({                      

map: map,                      

lineSymbol: measureSymbol,                      

defaultLengthUnit: Units.FEET                     },

dom.byId("measurementDiv"));                  

measurement.startup();                  

measurement.hideTool("location");                  

dojo.connect(measurement,"measure", function() {                      

alert('here');                  

});              

});

according to api "measure" is an event of Measurement https://developers.arcgis.com/javascript/jsapi/measurement-amd.html#event-measure

0 Kudos
1 Solution

Accepted Solutions
RyanSellman
Occasional Contributor II

I modified one of the samples from the API's site here and it seems to be working:

Edit fiddle - JSFiddle

View solution in original post

0 Kudos
8 Replies
RyanSellman
Occasional Contributor II

Hi John,

I was able to achieve this by grabbing code from Tim Witt's sample here Javascript API - Advanced Draw widget​.

Hope this helps!

Ryan

JohnPreston
Occasional Contributor

Thanks Ryan,

That is a pretty cool widget!

Do you know why the "measure" event is not firing in my situation?

0 Kudos
KenBuja
MVP Esteemed Contributor

When you use "dojo.connect" instead of "on", the event has a different name. Instead of "measure", it would be "onMeasure". You should get in the habit of using the "on" style. From the Events documentation:

Using the on method is preferable to using connect for several reasons. First, the Dojo documentation states that connect support will be removed in Dojo 2.0, and in fact, connect is now a legacy convenience wrapper for dojo.on. Secondly, especially in AMD style, it makes your code less verbose and is more similar to event attachment syntax in other JavaScript frameworks. Finally, for esri components we add a target property to all "synthetic" events (not mouse or key events) which points to the component which fired the event. Since event handlers fire asynchronously and there is no guarantee of the context in which the event handler will fire, the value of this is not dependable. However, the value of evt.targetis dependable.

The documentation for the connect style events of the Measurement dijit only lists "onMeasureEnd". I don't know if this is an oversight or not.

JohnPreston
Occasional Contributor

Thank you Ken,

I actually tried both ways. Maybe I'm confusing "on connect events" with "on style events." "measure" is an "on style event" while as you point out "OnMeasureEnd" is an "on connect event." "measure-end", "measure-start"  are other "on style events."

0 Kudos
JohnPreston
Occasional Contributor

For example:

This fires:

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

alert('here1');

});

This does not:

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

alert('here');

});

0 Kudos
RyanSellman
Occasional Contributor II

I modified one of the samples from the API's site here and it seems to be working:

Edit fiddle - JSFiddle

0 Kudos
JohnPreston
Occasional Contributor

Thank you Ryan,

That works great! I've spent the afternoon trying to figure out why your fiddle worked but not my map.

Your example is using ArcGIS version 3.15, while I am using 3.8. It seems odd that it wouldn't work but I changed my map to version 3.15 and it works fine.

0 Kudos
KenBuja
MVP Esteemed Contributor

The measure event wasn't introduced until version 3.11. This is noted in the documentation.