Get the coordinates of the free line of the drawn polyline by the user. (ArcGIS JavaScript API)

I am a newbie here and I have been using ArcGIS JavaScript for 2 weeks now.

I followed this example:

http://help.arcgis.com/en/webapi/javascript/arcgis/jssamples/#sample/graphics_add

And I managed to draw a free polyline on the map. Now I need to calculate the coordinates of every single point on this polyline. I've been trying to do this for about 5 days now and I can't figure out how to do it. I would appreciate it if any of you could explain to me how to do this in detail (since I don't have too much experience with it).

Thanks in advance,

Diego.

+3


source to share


1 answer


You need to check the object geometry

you are getting from the onDrawEnd event - this event is already connected in the example:

dojo.connect(tb, "onDrawEnd", addGraphic);

but all it does is add a line to the map with the base symbol. The first trap is that the geometry object will not actually be of a type geometry

- an abstract base class that does not actually exist. The type will depend on what tool you used to draw the shape - in your case it sounds like it would be a type Polyline

.



This Polyline object has a property paths

that is a nested array of paths (strings), each containing an array of points. So if you want to get all points of all paths in a Polyline object:

function addGraphic(geo) {
    //For each path...
    for ( var path = 0; path < geo.paths.length; path ++ ) {
        //For each point in the path...
        for ( var pt = 0; pt < geo.paths[path].length; pt++ ) {
            //Do something with each point in here...
            //X coordinate: geo.paths[path][pt][0]
            //Y coordinate: geo.paths[path][pt][1]
        }
    }
}

      

You probably want to do a few more checks on things like you have a polyline or some other object that inherits from geometry

, since the property that contains the actual coordinates varies between polyline, polygon, point, etc. ...

+2


source







All Articles