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:
a: Object
- paths: Array(3)
- 0: Array(2)
- 0: -111.3
- 1: 52.68
- length: 2
- __proto__: Array(0)
- 1: Array(2)
- 0: -98
- 1: 49.5
- length: 2
- __proto__: Array(0)
- 2: Array(2)
- 0: -93.94
- 1: 29.89
- length: 2
- __proto__: Array(0)
- length: 3
- __proto__: Array(0)
- spatialReference:
- wkid: 4326
- __proto__: Object
- type: "polyline"
- __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
Solved! Go to Solution.
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,
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]
]
});
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>
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;
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