This sample demonstrates how to use the geoprocessor asynchronously to calculate a hotspot analysis based on the frequency of 911 calls. It calculates the frequency of these calls within a given study area during a specified constrained time period set between 1/1/1998 and 5/31/1998. To run the hotspot analysis, click on the Analyze 911 Calls
button.
How it works
When the Analyze 911 Calls
button is clicked, an event listener triggers the function find
, which accesses the user-defined dates and passes it as a parameter to geoprocessor. The submit
method is subsequently called and performs asynchronously. This method returns immediately after the job is submitted with a JobInfo object that contains information relating to the state of the execution of geoprocessor. The waitForJobCompletion() method recognizes when the geoprocessor has finished processing, then calls the function to draw the result on the map.
const params = {
Query: buildDefinitionQuery()
};
geoprocessor.submitJob(gpUrl, params).then((jobInfo) => {
const options = {
statusCallback: (jobInfo1) => {
progTest(jobInfo1);
}
};
jobInfo.waitForJobCompletion(options).then((jobInfo2) => {
drawResultData(jobInfo2);
});
});
While the submit
method is running, the prog
function pushes updates from the method to the sidebar div.
function progTest(value) {
message.innerText = "Job status: " + "'" + value.jobStatus + "'";
}
The draw
callback function obtains the result as a MapImageLayer when the method completes successfully. It then adds the MapImageLayer to the map.
function drawResultData(result) {
// use the result (JobInfo object) to fetch a MapImageLayer
result.fetchResultMapImageLayer(result.jobId).then((resultLayer) => {
resultLayer.opacity = 0.7;
resultLayer.title = "HotspotLayer";
// add the result layer to the map
map.layers.add(resultLayer);
});
}