ArcGIS API for JavaScript 4.2 Learning notes [23] No map how do I query? "Use of the Findtask class"

Source: Internet
Author: User
Tags visibility

From the first to the present is based on the map, no matter how, whether it is 2D or 3D, there is at least one figure.

This time there is no diagram of the example, to see what the text of the spatial query.

This example applies to queries from a background query or a low-performance computer.

Preview Map

Since this example of 4.3 and 4.2 does not have any substantive changes, I directly run from the official 4.3 example:

By default, when you press the Find button, a circle is shown on the right, which is actually a GIF image.

As a result, the information Spokane This county is enumerated, and the fields listed are County Name, State, Population (2012), and%population Change (2000-2010) four.

The corresponding values are listed below with the regular font.

Isn't it simple? The code is not necessarily simple.

Give a reference

The reference on the HTML does not require CSS:

<src= "https://js.arcgis.com/4.2/"></script> 

Just refer to the JS library.

Function Reference:

require ([    "Esri/tasks/findtask",    "Esri/tasks/support/findparameters",    " Dojo/_base/array ",    " Dojo/dom ",    " dojo/on "    ," dojo/domready! "   ],       function (Findtask, Findparameters, Arrayutils, Dom, on) {         // code      });

Oh yo, Findtask actually did not use query, but to switch to Findparameters. It seems to be different from the words query, Find, search.

function parameter skeleton
function(Findtask, Findparameters, Arrayutils, Dom, on) {varloadingimg = Dom.byid ("Loading"); varFind =Newfindtask ({}); varparams =Newfindparameters ({}); functionDoFind () {}varresultstable = Dom.byid ("TBL"); functionShowresults (response) {}functionrejectedpromise (Err) {} on (Dom.byid ("Findbtn"), "click", DoFind);}

This is similar to the previous example, Findtask, Findparameters and Querytask, and query, which appear in pairs:

var New Findtask ({  "https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_2000-2010_ Population_change/mapserver "}); var New findparameters ({  layerids: [3],  searchfields: ["NAME"]});

It is worth mentioning that the Findtask class is also instantiated by a service published by Arcserver. Check the API to learn the details of the Findtask class:

Findtask

Same as Querytask, but the parameter is changed to Findparameters, and the return value becomes a variable of type findresults.

Findparameters

Something similar to query:

Lists several common properties: SearchText (String), Searchfields (string[]), Returngeometry (Boolean), Outspatialreference (spatialreference)

The first two are the attributes used in this example, SearchText is the text that the user enters to search for, and Searchfields is the field to search for. The latter two are the same as query.

Findresult

List several common properties: Feature (Graphic), Foundfieldname (String)

The previous one is the found geometry (one row in the attribute table), and the next one in the field where the search is set in the findparameters of the geometry.

The DoFind () and the Doquery () of the previous example are similar, the successful invocation of Showresults (), and the failure to invoke Rejectedpromise ().

The last one is to add the click event for the FINDBTN Dom button.

The logic is as follows, and the last example is almost a hair, just showresults this method of handling the returned result is not quite the same.

Lower energy!!
functionShowresults (response) {varResults =Response.results; Resultstable.innerhtml= ""; //If the returned results is empty, then tell the user  if(Results.length = = 0) {resultstable.innerhtml= "<i>no results found</i>"; LoadingImg.style.visibility= "hidden"; return; }  //HTML operation, add a row, add 4 cells  varToprow = Resultstable.insertrow (0); varCell1 = Toprow.insertcell (0); varCell2 = Toprow.insertcell (1); varCell3 = Toprow.insertcell (2); varCELL4 = Toprow.insertcell (3); //HTML operations, filling in values for each cellcell1.innerhtml = "<b>county name</b>"; Cell2.innerhtml= "<b>State</b>"; Cell3.innerhtml= "<b>population (</b>)"; Cell4.innerhtml= "<b>% Population change (2000-2010) </b>"; //! The most critical piece of code! Arrayutils.foreach (Results,function(Findresult, i) {varCounty =FindResult.feature.attributes.Name; varState =findresult.feature.attributes["State abbreviation"]; varPopgrowth =findresult.feature.attributes["2000-2010 Population Annual Compound growth rate (U.S. Census)"    ]; varpop2012 =findresult.feature.attributes["Total Population (ESRI)"]; //HTML operations. Add a row, add 4 cells, and write the value.     varrow = Resultstable.insertrow (i + 1); varCell1 = Row.insertcell (0); varCell2 = Row.insertcell (1); varCell3 = Row.insertcell (2); varCELL4 = Row.insertcell (3); Cell1.innerhtml=County; Cell2.innerhtml=State ; Cell3.innerhtml=pop2012; Cell4.innerhtml=popgrowth;    }); //circled GIF is not visibleloadingImg.style.visibility = "hidden";}

Although this code is very long, many are HTML DOM element operations, we separate the key section out:

 Arrayutils.foreach (results, function   ( Findresult, i) { //  Get the required field values from the Findresult object  var  County =  FindResult.feature.attributes.Name;  var  state = findresult.feature.attributes["state abbreviation" );  var  popgrowth = findresult.feature.attributes["2000-2010 Population  Annual Compound growth rate (U.S. Census) "";  var  pop2012 = findresult.feature.attributes["at Total Population (ESRI)  "";    //  HTML operation   ... omitted});  

Notice that the traversal is results, this thing is an object array, and is boxed into an Findresult array. This is actually getting the value of the feature attribute from the Findresult object, and then doing the HTML operation below.

So how did this results come about? Note the first sentence of the DoFind () method:

var results = response.results;

Gets the results property from the passed-in parameter response. After review, this response is not a findresult, but a parameter of type Portalqueryresult.

Portalqueryresult has a property of results, and the value type is an object array.

So now it's clear! This little place is quite a pit.

Sum up.

Use Findtask and findparameters for "find" operations, not "query queries", nor "search searches."

Reads the result Findresult and writes the result to the HTML element.

The whole example has no map, fresh and refined ... #滑稽.

ArcGIS API for JavaScript 4.2 Learning notes [23] No map how do I query? "Use of the Findtask class"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.