geometryService.lengths() is not working on the 4.8

1167
4
Jump to solution
09-20-2018 07:57 AM
RN
by
New Contributor II

Hello,

I've take the code from here :

ArcGIS API for JavaScript Sandbox 

And i've added this code

var lengthParamsCliped = new LengthsParameters();
lengthParamsCliped.calculationType = 'geodesic';
lengthParamsCliped.geodesic = true;
lengthParamsCliped.lengthUnit = 'METER';
lengthParamsCliped.polylines = [polyline];

geometryService.lengths(lengthParamsCliped).then(calculatedClippedLength);

function calculatedClippedLength(res)
{
console.info(res);
}

The code stop with the message "a.toJSON is not a function"

Where a is the polyline who looks like that:

  1. a: Object
    1. pathsArray(3)
      1. 0Array(2)
        1. 0-111.3
        2. 152.68
        3. length2
        4. __proto__Array(0)
      2. 1Array(2)
        1. 0-98
        2. 149.5
        3. length2
        4. __proto__Array(0)
      3. 2Array(2)
        1. 0-93.94
        2. 129.89
        3. length2
        4. __proto__Array(0)
      4. length3
      5. __proto__Array(0)
    2. spatialReference:
      1. wkid4326
      2. __proto__Object
    3. type"polyline"
    4. __proto__Object

I will be grateful if someone could help me to find a solution.

Thanks in advance

Best regards

RN

#geometryService

#lengths()

#4.8

#4.x

#a.toJSON

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

R N,

   The doc is not very clear when it say a string or number for the length unit it is expecting the numeric value of the unit as a number or a string. METER = 9001

lengthParamsCliped.lengthUnit = 9001;

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

RN,

  The LengthsParameters class does not autoCast the values of the polylines property to actual PolyLines. So you have to give it an array of ACTUAL polylines, not json representing Polylines.

      // First create a line geometry (this is the Keystone pipeline)
      var polyline = new Polyline({
        type: "polyline",
        paths: [
          [-111.30, 52.68],
          [-98, 49.5],
          [-93.94, 29.89]
        ]
      });
RN
by
New Contributor II

Ok i've done that and its better because i dont have any errors but unfortunaly i dont have any result at all.

Im using a simple code like that just to understand how i can mesure the lenght of a polyline.

Please take a look here: 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Intro to graphics - 4.8</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.8/esri/css/main.css">
<script src="https://js.arcgis.com/4.8/"></script>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/tasks/GeometryService",
"esri/tasks/support/LengthsParameters",
"esri/geometry/SpatialReference",
"esri/geometry/Polyline",
"esri/Graphic",
"dojo/domReady!"
], function(
Map, MapView, GeometryService, LengthsParameters, SpatialReference, Polyline, Graphic
) {
var map = new Map({
basemap: "topo"
});
var view = new MapView({
center: [-80, 35],
container: "viewDiv",
map: map,
zoom: 3
});
var geometryService = new GeometryService('http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer');
/****************************
* Create a polyline graphic
****************************/
// First create a line geometry (this is the Keystone pipeline)
var polyline = new Polyline({
type: "polyline",
paths: [
[-111.30, 52.68],
[-98, 49.5],
[-93.94, 29.89]
],
spatialReference: { wkid: 4326 }
});
// Create a symbol for drawing the line
var lineSymbol = {
type: "simple-line", // autocasts as SimpleLineSymbol()
color: [226, 119, 40],
width: 4
};
var polylineGraphic = new Graphic({
geometry: polyline,
symbol: lineSymbol
});
view.graphics.add(polylineGraphic);
// try to find the lenght of a polyline
var myLongueur;
var lengthParamsCliped = new LengthsParameters();
lengthParamsCliped.calculationType = 'geodesic';
lengthParamsCliped.geodesic = true;
lengthParamsCliped.lengthUnit = 'METER';
lengthParamsCliped.polylines = [polyline];

geometryService.lengths(lengthParamsCliped).then(function (calculatedClippedLength) {

if (calculatedClippedLength.lengths.length > -1) {
myLongueur = calculatedClippedLength.lengths[0];
console.info(myLongueur);
}
});
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

R N,

   The doc is not very clear when it say a string or number for the length unit it is expecting the numeric value of the unit as a number or a string. METER = 9001

lengthParamsCliped.lengthUnit = 9001;
RN
by
New Contributor II

You have make my weekend better, i've tried so hard to find a solution and i've knew it was something little to find.

Best regards

RN

0 Kudos