AnsweredAssumed Answered

Nesting DOJO Asyn calls not working,wait until all are complete

Question asked by shaikhrizuan on Jun 26, 2018
Latest reply on Jun 27, 2018 by mirhashmi2014

Hi,

I have to get the distance from various points plotted on the map from the user point on the page load. When i am keeping all the promise in an array, and assigning to dojo/promise/all, it throws an error "Unable to complete operation." below is the code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 

<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Create Map and add a dynamic layer</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.24/esri/css/esri.css"/>
<style>
html, body, #mapDiv{
padding: 0;
margin: 0;
height: 100%;
}
</style>
<script src="https://js.arcgis.com/3.24/"></script>
<script>
var map;
var fromPoint;
var task;
var toPoints=[];
require([
"dojo/_base/lang",
"esri/map",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/tasks/GeometryService",
"esri/layers/GraphicsLayer",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/Color",
"esri/symbols/SimpleMarkerSymbol",
"esri/geometry/Point",
"esri/SpatialReference",
"esri/graphic", 
"esri/tasks/DistanceParameters",
"dojo/_base/array",
"dojo/promise/all"
], function (
lang, Map, ArcGISDynamicMapServiceLayer, GeometryService,GraphicsLayer,SimpleFillSymbol,SimpleLineSymbol,Color,SimpleMarkerSymbol,Point,SpatialReference,Graphic,DistanceParameters,array,all) {

map = new Map("mapDiv", {
sliderOrientation : "horizontal"
});

//Takes a URL to a non cached map service.
var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("https://servername:6443/arcgis/rest/services/IndiaMapMMI/MapServer", {
});
map.addLayer(dynamicMapServiceLayer);
var geometryService = new esri.tasks.GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
var frmGraphicsLayer = new GraphicsLayer();
var toGraphicsLayer = new GraphicsLayer();
var circleSymbF = new SimpleFillSymbol( 
SimpleFillSymbol.STYLE_SOLID, 
new SimpleLineSymbol( 
SimpleLineSymbol.STYLE_SHORTDASHDOTDOT, 
new Color([255, 0, 0]),2), new Color([255, 255, 0, 0.25]) 
); 
var symbol2 = new SimpleMarkerSymbol( 
SimpleMarkerSymbol.STYLE_DIAMOND, 
15, 
new SimpleLineSymbol( 
SimpleLineSymbol.STYLE_NULL, 
new Color([0, 0, 0, 0.9]), 
1 
), 
new Color([255, 0, 0, 1]) 
); 
var symbol1 = new SimpleMarkerSymbol( 
SimpleMarkerSymbol.STYLE_DIAMOND, 
15, 
new SimpleLineSymbol( 
SimpleLineSymbol.STYLE_NULL, 
new Color([0, 0, 0, 0.9]), 
1 
), 
new Color([0, 255, 0, 1]) 
); 
var symbol3 = new SimpleMarkerSymbol( 
SimpleMarkerSymbol.STYLE_DIAMOND, 
15, 
new SimpleLineSymbol( 
SimpleLineSymbol.STYLE_NULL, 
new Color([0, 0, 0, 0.9]), 
1 
), 
new Color([0, 0, 255, 1]) 
); 
myObj = {"features":[

 

{
"attributes": { 
"CaseID": 001, 
"Latitude": 28.634962,
"Longitude": 77.034805,
"SerialNo":7557,
"Status":"FromPoint"
}
},
{
"attributes": { 
"CaseID": 002, 
"Latitude": 28.612663,
"Longitude": 77.053215,
"SerialNo":7557,
"Status":"Pending"
}
},
{
"attributes": {
"CaseID": 003, 
"Latitude": 28.596744,
"Longitude": 77.075981,
"SerialNo":7557,
"Status":"Completed"
}
},{
"attributes": {
"CaseID": 004,
"Latitude": 28.615241,
"Longitude": 77.061687,
"SerialNo":7557,
"Status":"Completed"
}
} 
]
}; 
var promises = [];

 

for(i = 0 ; i <myObj.features.length; i++){
if(myObj.features[i].attributes.Status == "FromPoint"){
fromPoint = new Point(parseFloat(myObj.features[i].attributes.Longitude),parseFloat(myObj.features[i].attributes.Latitude),new SpatialReference({wkid:4326}));
frmGraphicsLayer.add((new Graphic(fromPoint,symbol2)));
}
else{

 

var pt = new Point(parseFloat(myObj.features[i].attributes.Longitude),parseFloat(myObj.features[i].attributes.Latitude),new SpatialReference({wkid:4326}));
toPoints.push(pt);
}
}
task = array.map(toPoints,lang.hitch(this, function (toPoint) {
var distParams = new esri.tasks.DistanceParameters();
distParams.distanceUnit = esri.tasks.GeometryService.UNIT_SQUARE_METERS;

distParams.geometry1 = fromPoint;
distParams.geometry2 = toPoint;
distParams.geodesic = true;
return distParams;
}));
for (i = 0; i < task.length; i++) {
promises.push(geometryService.distance(task[i]));
}
var iPromises = new all(promises);
iPromises.then(lang.hitch(this, function (distance) {
if (isNaN(distance)) {
distance = 0;
}
if(distance >70){
toGraphicsLayer.add((new Graphic(pt,symbol3)));
}
else{
toGraphicsLayer.add((new Graphic(pt,symbol1)));
}
}), lang.hitch(this, function (err){
console.info(err);
}));
map.addLayer(frmGraphicsLayer);
map.addLayer(toGraphicsLayer); 
});
</script>
</head>
<body>
<div id="mapDiv"></div>
</body>
</html>

 

i have referred this thread How to nest dojo async calls and wait until all are complete  but no success

Outcomes