You are here: > ESRI Forums > arcgis server forums > Thread Replies

ArcGIS Server Forums

ArcGIS API for JavaScript forum

Can I add segment measurement for measure d...   Cindy Lu Jul 13, 2009
Re: Can I add segment measurement for measu...   John Fletcher Jul 13, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Jul 14, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Jul 14, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Jul 14, 2009
Re: Can I add segment measurement for measu...   John Fletcher Jul 14, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Jul 14, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Jul 14, 2009
Re: Can I add segment measurement for measu...   John Fletcher Jul 14, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Jul 15, 2009
Re: Can I add segment measurement for measu...   John Fletcher Jul 15, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Jul 16, 2009
Re: Can I add segment measurement for measu...   Jason Zou Aug 28, 2009
Re: Can I add segment measurement for measu...   Cindy Lu Aug 30, 2009
Re: Can I add segment measurement for measu...   Jason Zou Aug 31, 2009
Re: Can I add segment measurement for measu...   Tom Konzel Nov 30, 2009
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 13, 2009 
Message Hello,

I want to add measure segments functionality in measure distance function. I want to measure distance of a POLYLINE, but will display the distance of each segment, get the total length for polyline after user double click.

What is the event should I use?

Thank you!

Cindy

 
 
============================================
    <script type="text/javascript">
      dojo.require("esri.map");
      dojo.require("esri.tasks.geometry");
      dojo.require("esri.toolbars.draw");
      dojo.require("dojo.number");

      var geometryService;

      function init() {
        //identify proxy page to use if the toJson payload to the geometry service is greater than 2000 characters.
        //If this null or not available the project and lengths operation will not work.  Otherwise it will do a http post to the proxy.
        esriConfig.defaults.io.proxyUrl = "/arcgisserver/apis/javascript/proxy/proxy.ashx";
        esriConfig.defaults.io.alwaysUseProxy = false;

        var startExtent = new esri.geometry.Extent(-80.0571,41.3697,-74.4321,44.0822, new esri.SpatialReference({wkid:4326}) );
        var map = new esri.Map("map", {extent:startExtent});
        dojo.connect(map, "onLoad", initFunctionality);

        map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"));

        geometryService = new esri.tasks.GeometryService("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

        dojo.connect(geometryService, "onLengthsComplete", outputDistance);
        dojo.connect(geometryService, "onProjectComplete", function(graphics) {
          //call GeometryService.lengths() with projected geometry
          geometryService.lengths(graphics);
        });
      }

      function initFunctionality(map) {
        var tb = new esri.toolbars.Draw(map);

        //on draw end add graphic, project it, and get new length
        dojo.connect(tb, "onDrawEnd", function(geometry) {
          //map.graphics.clear();
          var graphic = map.graphics.add(new esri.Graphic(geometry, new esri.symbol.SimpleLineSymbol()));
          //add graphic to the map and then call GeometryService.project() to get graphic into a coordinate system that
          //can be used to generate an accurate length.
          geometryService.project([graphic],new esri.SpatialReference({"wkid":32618}));
        });
        tb.activate(esri.toolbars.Draw.POLYLINE);
      }

      function outputDistance(result) {
        dojo.byId("distance").innerHTML = dojo.number.format(result.lengths[0] / 1000) + " kilometers";
      }

      dojo.addOnLoad(init);
    </script>
=============================
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author John Fletcher 
Date Jul 13, 2009 
Message Cindy,

Your outputDistance function is reporting only the length of the first segment of your polyline (result.lengths[0]). If you loop through the lengths array instead, you can output all of the individual segment lengths. While looping through, sum the segment lengths then after your loop you'll have the total length of the polyline. 
  John Fletcher
Latitude Geographics
www.geocortex.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 14, 2009 
Message Hello John,

Thank you.

I tried to loop through the result.lengths. There are two issues here:
First, the result.lengths.length alway =1 for POLYLINE,LINE,FREEHAND_POLYLINE.
Second, the outputDistance function only be called after the onLengthsComplete event. That means it would not return me the length of each segment before the drawing was done.

Any thoughts on that?

Thank you!

Cindy Lu

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 14, 2009 
Message FOR EXAMPLE: This is my polyline with 3 segments. I want length of each segment and when onLengthsComplete, get the total length of this polyline.

\_______/


Thank you

Cindy 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 14, 2009 
Message \_________/ Polyline with 3 segments 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author John Fletcher 
Date Jul 14, 2009 
Message Cindy,

I read the API wrong...sorry about that. Before sending the polyline to the GeometryService it'll need to be broken up into individual 2-point polyline objects. Try something like the code below. It's not tested, and I'm actually not sure about a few things - for example, a)whether I've assumed the correct structure for the Number array represented by the paths property and b)whether each path in the polyline is in fact a 2-point line segment. Note that this will also only work for single projected polylines as currently written. You hopefully can work with this though... 
 
dojo.connect(geometryService, "onProjectComplete", function(graphics) {
  var graphicsArray = [], pl;
  for (var i = 0; i < graphics[0].paths.length; i++) {
    pl = new esri.geometry.Polyline(new esri.SpatialReference({"wkid":32618}));
    pl.addPath(graphics[0].paths[i]);
    graphicsArray.push(pl);
  }

  geometryService.lengths(graphicsArray);
});
 
  John Fletcher
Latitude Geographics
www.geocortex.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 14, 2009 
Message John,

Thanks.

'graphics[0].paths.length' in your code should be 'graphics[0].geometry.paths[0].length'. But this would solve the second problem - give me the lenght of the segment after each click before the event onProjectComplete.

I guess I will need to draw points after each click instead, then construct line for every two points and get the length. At the end, get the total length of polyline.

Cindy Lu 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 14, 2009 
Message "this would solve the second problem"
should be
"this wouldnot solve the second problem" 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author John Fletcher 
Date Jul 14, 2009 
Message Cindy,

Looking at it a bit more, you may be able to use the following event (make sure you disconnect when the drawing is done though...). It looks like it may fire on each segment added. 
 
dojo.connect(map.graphics, "onGraphicAdd", function(graphic) {
  geometryService.project([graphic],new esri.SpatialReference({"wkid":32618}));
});
 
  John Fletcher
Latitude Geographics
www.geocortex.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 15, 2009 
Message The onGraphicAdd only be fired after the whole POLYLINE was added. I am thinking use
dojo.connect(map,"onCLick",function(evt) {});
But sounds like I can not get the graphic parameter for distance calculation.

Cindy 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author John Fletcher 
Date Jul 15, 2009 
Message Cindy,

I hesitate to suggest this, because it accesses a private variable which is normally a bad thing, but the following should work (inside your initFunctionality method): 
 
dojo.connect(map, "onClick", function() {
  if (tb._graphic) {
    geometryService.project([tb._graphic], new esri.SpatialReference({"wkid":32618}));
  }
});
 
  John Fletcher
Latitude Geographics
www.geocortex.com 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Jul 16, 2009 
Message Ok, John. With this piece of code, it works perfect. For other developer's reference, put this piece of code after - tb.activate(esri.toolbars.Draw.POLYLINE);

Thanks!

Cindy 
 
tb.activate(esri.toolbars.Draw.POLYLINE);

        dojo.connect(map, "onClick", function() {
            if (tb._graphic) {
                geometryService.project([tb._graphic], new esri.SpatialReference({"wkid":32618}));
            }
        });

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Jason Zou 
Date Aug 28, 2009 
Message Hi,

I am running into the similar issue. Currently the measurement tool for polyline on our site can only calculate the total length of the finished polyline. But the user likes the IMS behavior, which shows two values, the first value showing the total length of the path formed by all the clicks, the other showing the length of the last moving line segment, which is between the mouse position and the last click point.

But as you mentioned, drawToolbar only has one event handler, onDrawEnd. Your solution will work for calculating each static segment and their total which is great. But it cannot be used to calculate for the last moving one.

Any idea? Thanks,

Jason Zou 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Cindy Lu 
Date Aug 30, 2009 
Message I can only calculate the length of the last segment. But won't be able to get the length of the last moving line( when mouse is keeping on moving). Yes, I like this kind of behavior too. You maybe post your question as a new post and see whether some experts have any idea.

For the length of the last segment, I put a variable total to remember the last total length of the polyline. Then use
dojo.number.format(result.lengths[0] / 1000 - total
to get the length of the last segment.

Cindy 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Jason Zou 
Date Aug 31, 2009 
Message Cindy,

Thanks for your response. By using the hidden _graphic property of toolbar varialbe, we can get the length of the each clicked line segment. But _graphic seems not hosting the moving line. I just could not find which magic variable has the info. I will take your suggestion, and publish it as a new post.

Thank you,

Jason 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Can I add segment measurement for measure distance function? 
Author Tom Konzel 
Date Nov 30, 2009 
Message Hello Jason, was there ever a solution to determining the current segment length before the click? We would still like to implement this function to mimic the behavior of the ArcIMS measure tool. 
  Thomas Konzel
thomas.konzel@arlingtontx.gov