Closest Facility Service with Asynchronous Execution

2605
5
Jump to solution
07-25-2013 05:15 PM
ShaunWeston
Occasional Contributor
I've been trying to solve a closest facility task with asynchronous execution, but not having much luck so far. It's works fine for synchronous excecution

e.g. The below works fine on http://route.arcgis.com/arcgis/rest/services/World/ClosestFacility/NAServer/ClosestFacility_World
but not
http://logistics.arcgis.com/arcgis/rest/services/World/ClosestFacility/GPServer/FindClosestFacilitie...


// Push map click into incident parameter var features = []; features.push(location); var incidents = new esri.tasks.FeatureSet(); incidents.features = features; closestFacilityParams.incidents = incidents;   var inPoint = new esri.geometry.Point("1719868", "6047566", app.map.spatialReference); var location = new esri.Graphic(inPoint, configOptions.markerSymbol); var features = []; features.push(location); var facilities = new esri.tasks.FeatureSet(); facilities.features = features; closestFacilityParams.facilities = facilities;   closestFacilityTask.solve(closestFacilityParams, function (result) {    alert(result.routes.length); });


The reason I need use the async service is I have over 100 facilities.
0 Kudos
1 Solution

Accepted Solutions
ShaunWeston
Occasional Contributor
Yeah I did manage to get it working. This is the function I used.

// Locate the closest feature function getClosestFeature(evt) {     // Setup the report progress bar and show it     $("#closestFeatureLoadBar").progressbar({         value: false     });     $("#closestFeatureLoadBar").show();       // Clear previous grpahics     app.map.graphics.clear();       // Get the map click point and add it as a location     var inPoint = new esri.geometry.Point(evt.mapPoint.x, evt.mapPoint.y, app.map.spatialReference);     var location = new esri.Graphic(inPoint, configOptions.markerSymbol);     // Add map click to map     app.map.graphics.add(location);     // Push map click into incident parameter     var features = [];     features.push(location);     var incidents = new esri.tasks.FeatureSet();     incidents.features = features;         // Get the facility features from the map service     var dataLayer = new esri.tasks.DataFile({         url: configOptions.facilitiesURL + "/query?where=1%3D1&returnGeometry=true&outFields=*&f=json"     });       // Setup the parameters for the closest facility     var closestFacilityParams = {         "incidents": incidents,         "facilities": dataLayer,         "cutoff": configOptions.cutOffTime,         "route_shape": "True Shape",         "populate_directions": true,         "directions_language": "en",         "directions_distance_units": "Meters",         "directions_style_name": "NA Desktop"     }       // Setup closest facility task     // If service is secure get token     var closestFacilityTask;     if (configOptions.closestFacilityService.secure === "true" || configOptions.closestFacilityService.secure === true) {         // Make the request         getToken(configOptions.closestFacilityService.tokenURL, configOptions.closestFacilityService.username, configOptions.closestFacilityService.password, function (token) {             closestFacilityTask = new esri.tasks.Geoprocessor(configOptions.closestFacilityService.url + "?token=" + token);             closestFacilityTask.outSpatialReference = app.map.spatialReference;             // Submit the closest facility tool task             closestFacilityTask.submitJob(closestFacilityParams, completeCallback);         });     }     else {         closestFacilityTask = new esri.tasks.Geoprocessor(configOptions.closestFacilityService.url);         closestFacilityTask.outSpatialReference = app.map.spatialReference;         // Submit the closest facility tool task         closestFacilityTask.submitJob(closestFacilityParams, completeCallback, taskFailed);     }       // When task is complete     function completeCallback(jobInfo) {         // If job completed successfully         if (jobInfo.jobStatus !== "esriJobFailed") {             // When route and directions returned             closestFacilityTask.getResultData(jobInfo.jobId, "output_routes", getRoute);             closestFacilityTask.getResultData(jobInfo.jobId, "output_directions", getDirections);         }             // If failed         else {             // Show the error message - Get from job info messages             alert(configOptions.errorMessageText + jobInfo.messages[6].description);         }         // Hide progress bar         $("#closestFeatureLoadBar").hide();           // Go back to identify mode         clickmode = 'identify';         changeCursor(clickmode);     }       // Get route result     function getRoute(result) {         // For each of the features returned         $.each(result.value.features, function () {             // Add facility to map             var layerOID = this.attributes.FacilityID;             var queryTask = new esri.tasks.QueryTask(configOptions.facilitiesURL);             var query = new esri.tasks.Query();             query.where = "OBJECTID = " + layerOID;             query.outSpatialReference = app.map.spatialReference;             query.returnGeometry = true;             query.outFields = ["*"];             queryTask.execute(query, function (results) {                 // For each of the results returned                 $.each(results.features, function () {                     var facility = new esri.Graphic(this.geometry, configOptions.markerSymbol);                     // Add point to map                     app.map.graphics.add(facility);                 });             });               // Add the route to the map             var route = new esri.geometry.Polyline(this.geometry);             var graphic = new esri.Graphic(route, configOptions.routeLineSymbol);             app.map.graphics.add(graphic);               // Zoom to feature and expand out extent             var extent = graphic.geometry.getExtent();             app.map.setExtent(extent.expand(configOptions.routeZoomBuffer));         });     }     // Get directions result     function getDirections(result) {         // For each of the features returned         var directionsText = "";         var count = 0;         $.each(result.value.features, function () {             var directions = this.attributes.Text;             var distance = Math.round(this.attributes.DriveDistance * 100) / 100;             var time = Math.round(this.attributes.ElapsedTime * 100) / 100;               // If not the first line then add paragraph and directions             if (count != 0) {                 directionsText = directionsText + "</br></br><B>" + directions + "</B>";                   // Add in distance and time if necessary                 if (distance > 0) {                     directionsText = directionsText + "</br>" + distance + " metres";                 }                 if (time > 0) {                     directionsText = directionsText + "</br>" + time + " minutes";                 }             }                 // Add starting text             else {                 directionsText = directionsText + "<B>Starting from your location</B>";             }             count = count + 1;         });           // // Populate and show info window - Desktop version         if (currentPage.indexOf("map") != -1) {             app.map.infoWindow.hide();             // Size of infoWindow             app.map.infoWindow.resize(configOptions.infowWindowSize[0], configOptions.infowWindowSize[1]);             app.map.infoWindow.setTitle("Directions");               // Set location of info window             var screenPoint = new esri.geometry.ScreenPoint((app.map.width / 1.5), (app.map.height / 2));               // Set info window content                        app.map.infoWindow.setContent("<div id='directionsContent'></div>");             $("#directionsContent").append(directionsText);             app.map.infoWindow.show(screenPoint, app.map.getInfoWindowAnchor(screenPoint));         }     } }

View solution in original post

0 Kudos
5 Replies
ShaunWeston
Occasional Contributor
Ok because it's a GP service I think it needs to be implemented differently in the Javascript code. I've been looking through this:

http://resources.arcgis.com/en/help/arcgis-rest-api/#/Closest_Facility_Service_with_Asynchronous_Exe...

oes anyone have any samples where they've successfully consumed a AGS Online async network service with Javascript?
0 Kudos
sameerpuppal
Occasional Contributor

Hey Shaun,

Thanks to your sample I am able to use it for my routing with async call...it works well for 9 points but as soon as I use provide more than 9 points to async call... my calls starts through proxy. Did you face similar issue for more no. of points.

0 Kudos
SakshiNigotia
New Contributor
Hey. Did you get the resolution for this?
0 Kudos
ShaunWeston
Occasional Contributor
Yeah I did manage to get it working. This is the function I used.

// Locate the closest feature function getClosestFeature(evt) {     // Setup the report progress bar and show it     $("#closestFeatureLoadBar").progressbar({         value: false     });     $("#closestFeatureLoadBar").show();       // Clear previous grpahics     app.map.graphics.clear();       // Get the map click point and add it as a location     var inPoint = new esri.geometry.Point(evt.mapPoint.x, evt.mapPoint.y, app.map.spatialReference);     var location = new esri.Graphic(inPoint, configOptions.markerSymbol);     // Add map click to map     app.map.graphics.add(location);     // Push map click into incident parameter     var features = [];     features.push(location);     var incidents = new esri.tasks.FeatureSet();     incidents.features = features;         // Get the facility features from the map service     var dataLayer = new esri.tasks.DataFile({         url: configOptions.facilitiesURL + "/query?where=1%3D1&returnGeometry=true&outFields=*&f=json"     });       // Setup the parameters for the closest facility     var closestFacilityParams = {         "incidents": incidents,         "facilities": dataLayer,         "cutoff": configOptions.cutOffTime,         "route_shape": "True Shape",         "populate_directions": true,         "directions_language": "en",         "directions_distance_units": "Meters",         "directions_style_name": "NA Desktop"     }       // Setup closest facility task     // If service is secure get token     var closestFacilityTask;     if (configOptions.closestFacilityService.secure === "true" || configOptions.closestFacilityService.secure === true) {         // Make the request         getToken(configOptions.closestFacilityService.tokenURL, configOptions.closestFacilityService.username, configOptions.closestFacilityService.password, function (token) {             closestFacilityTask = new esri.tasks.Geoprocessor(configOptions.closestFacilityService.url + "?token=" + token);             closestFacilityTask.outSpatialReference = app.map.spatialReference;             // Submit the closest facility tool task             closestFacilityTask.submitJob(closestFacilityParams, completeCallback);         });     }     else {         closestFacilityTask = new esri.tasks.Geoprocessor(configOptions.closestFacilityService.url);         closestFacilityTask.outSpatialReference = app.map.spatialReference;         // Submit the closest facility tool task         closestFacilityTask.submitJob(closestFacilityParams, completeCallback, taskFailed);     }       // When task is complete     function completeCallback(jobInfo) {         // If job completed successfully         if (jobInfo.jobStatus !== "esriJobFailed") {             // When route and directions returned             closestFacilityTask.getResultData(jobInfo.jobId, "output_routes", getRoute);             closestFacilityTask.getResultData(jobInfo.jobId, "output_directions", getDirections);         }             // If failed         else {             // Show the error message - Get from job info messages             alert(configOptions.errorMessageText + jobInfo.messages[6].description);         }         // Hide progress bar         $("#closestFeatureLoadBar").hide();           // Go back to identify mode         clickmode = 'identify';         changeCursor(clickmode);     }       // Get route result     function getRoute(result) {         // For each of the features returned         $.each(result.value.features, function () {             // Add facility to map             var layerOID = this.attributes.FacilityID;             var queryTask = new esri.tasks.QueryTask(configOptions.facilitiesURL);             var query = new esri.tasks.Query();             query.where = "OBJECTID = " + layerOID;             query.outSpatialReference = app.map.spatialReference;             query.returnGeometry = true;             query.outFields = ["*"];             queryTask.execute(query, function (results) {                 // For each of the results returned                 $.each(results.features, function () {                     var facility = new esri.Graphic(this.geometry, configOptions.markerSymbol);                     // Add point to map                     app.map.graphics.add(facility);                 });             });               // Add the route to the map             var route = new esri.geometry.Polyline(this.geometry);             var graphic = new esri.Graphic(route, configOptions.routeLineSymbol);             app.map.graphics.add(graphic);               // Zoom to feature and expand out extent             var extent = graphic.geometry.getExtent();             app.map.setExtent(extent.expand(configOptions.routeZoomBuffer));         });     }     // Get directions result     function getDirections(result) {         // For each of the features returned         var directionsText = "";         var count = 0;         $.each(result.value.features, function () {             var directions = this.attributes.Text;             var distance = Math.round(this.attributes.DriveDistance * 100) / 100;             var time = Math.round(this.attributes.ElapsedTime * 100) / 100;               // If not the first line then add paragraph and directions             if (count != 0) {                 directionsText = directionsText + "</br></br><B>" + directions + "</B>";                   // Add in distance and time if necessary                 if (distance > 0) {                     directionsText = directionsText + "</br>" + distance + " metres";                 }                 if (time > 0) {                     directionsText = directionsText + "</br>" + time + " minutes";                 }             }                 // Add starting text             else {                 directionsText = directionsText + "<B>Starting from your location</B>";             }             count = count + 1;         });           // // Populate and show info window - Desktop version         if (currentPage.indexOf("map") != -1) {             app.map.infoWindow.hide();             // Size of infoWindow             app.map.infoWindow.resize(configOptions.infowWindowSize[0], configOptions.infowWindowSize[1]);             app.map.infoWindow.setTitle("Directions");               // Set location of info window             var screenPoint = new esri.geometry.ScreenPoint((app.map.width / 1.5), (app.map.height / 2));               // Set info window content                        app.map.infoWindow.setContent("<div id='directionsContent'></div>");             $("#directionsContent").append(directionsText);             app.map.infoWindow.show(screenPoint, app.map.getInfoWindowAnchor(screenPoint));         }     } }
0 Kudos
KenBuja
MVP Esteemed Contributor
Shaun,

You should mark your post as the answer to the question.
0 Kudos