Query and retrieval is an indispensable part of ArcGIS runtime for Android. This section describes two common query and retrieval tasks: identifytask and querytask. Identifytask is used to identify elements in layers, while querytask is used to query layer elements.
1 identifytask
1.1 features
Identifytask is a recognition task class. Simply put, when we click a map by fingers, we can obtain the information about the elements on the ground. If we want to obtain the information about the elements normally, before the identification operation, you must set a set of parameter information for the identifytask. The input parameter accepted by the identifytask must be an object of the identifyparameters type. In the identifyparameters object, you can set the corresponding recognition conditions.
Identifytask is used to identify multiple layers in the service. The returned result is an identifyresult [] array, and the task has three modes:
L
All_layers
This pattern indicates retrieving elements of all layers on the service during recognition.
L
Visible_layers
This mode indicates that only visible layer elements on the service are retrieved during recognition.
L
Top_most_layer
This mode indicates that only the top-level elements of the service are retrieved during identification.
Common identifyparameters interfaces:
Serial number |
Interface |
Description |
1 |
Setdpi(int dpi) |
Set the resolution value of Map |
2 |
Setgeometry( Geometry geometry) |
Set space ry object |
3 |
Setlayermode(int layerMode) |
There are three models:All_layers, visible_layers, and top_most_layer |
4 |
Setlayers(int[] layers) |
Set an array of recognized Layers |
5 |
Setmapextent( Envelope extent) |
Set the range of the current map |
6 |
Setmapheight(int height) |
Set the height of the map |
7 |
Setmapwidth(int width) |
Set the map width |
8 |
Setreturngeometry(boolean returnGeometry) |
Specify whether to return geometric objects |
9 |
Setspatialreference( Spatialreference spatialReference) |
Set space reference |
10 |
Settolerance(int tolerance) |
Set the recognized Tolerance Value |
1.2
Example
The following sample code describes how to use identifytask:
Params = new identifyparameters (); // identify the parameter object Params required by the task. settolerance (20); // sets the Params tolerance. setdpi (98); // set the dpiparams of the map. setlayers (New int [] {4}); // sets the layer array Params to be recognized. setlayermode (identifyparameters. all_layers); // sets the recognition mode // adds a click event listener map for the map. setonsingletaplistener (New onsingletaplistener () {Private Static final long serialversionuid = 1l; Public void onsingletap (final float X, final float y) {If (! Map. isloaded () {return;} // establish the identify parameterspoint identifypoint = map. tomappoint (x, y); Params. setgeometry (identifypoint); // sets the recognition location Params. setspatialreference (map. getspatialreference (); // sets the coordinate system Params. setmapheight (map. getheight (); // set the map pixel height to Params. setmapwidth (map. getwidth (); // set the map pixel width envelope Env = new envelope (); map. getextent (). queryenvelope (ENV); Params. setmapextent (ENV); // set the current map range to myiden. Tifytask mtask = new myidentifytask(identifypoint;;mtask.exe cute (Params );}});........................... Private class myidentifytask extends asynctask <identifyparameters, void, identifyresult []> {identifytask midentifytask; point manchor; myidentifytask (point anchorpoint) {manchor = anchorpoint ;} @ overrideprotected identifyresult [] doinbackground (identifyparameters... params) {identifyresult [] mresult = NULL; If (Params! = NULL & Params. length> 0) {identifyparameters mparams = Params [0]; try {mresult = midentifytask.exe cute (mparams); // execute the recognition task} catch (exception E) {// todo auto-generated catch blocke. printstacktrace () ;}} return mresult ;}@ overrideprotected void onpreexecute () {midentifytask = new identifytask ("http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer ");//}}
Through the above code, we can know that we need to perform the following steps in performing the recognition task:
1) The parameter object identifyparameters required to create an identification task
2) Set recognition conditions for parameter objects
3) define the myidentifytask class and inherit asynctask
4) execute () of the identifytask in the doinbackground () method of myidentifytask ();
Note: In the above example, our recognition task is executed in the asynctask subclass, because the recognition task request is an irregular operation, this class is used for asynchronous execution of recognition tasks to avoid affecting operations in the UI.
2 querytask
2.1 features
Querytask refers to a query task, which is a frequently used query method during development. querytask query tasks are very easy to use, the task only queries a layer in the service. Before executing the querytask task, it needs a query parameter object. This parameter mainly contains the query condition settings. Querytask allows you to query attributes, space, and attributes and space.
Common Query Interfaces:
Serial number |
Interface |
Description |
1 |
Setgeometry( Geometry geometry) |
Set space ry object |
2 |
Setinspatialreference( Spatialreference inSR) |
Set input space reference |
3 |
Setobjectids(int[] objectIds) |
Set the objectid array of the elements to be queried |
4 |
Setoutfields( String[] outFields) |
Set the array of returned Fields |
5 |
Setoutspatialreference( Spatialreference outSR) |
Reference for setting output space |
6 |
Setreturngeometry(boolean returnGeometry) |
Set whether to return geometric objects |
7 |
Setreturnidsonly(boolean returnIdsOnly) |
Set whether to return only the objiectid Field |
8 |
Setspatialrelationship( Spatialrelationship spatialRelationship) |
Set the queried spatial relationship |
9 |
Setwhere( String where) |
Set query Conditions |
2.2 example
The following sample code shows how to use querytask:
Targetserverurl = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer"; string targetlayer = targetserverurl. concat ("/3"); // service layer string [] queryparams = {targetlayer, "avghhsz_cy> 3.5"}; asyncquerytask ayncquery = new asyncquerytask(~~ayncquery.exe cute (queryparams ); private class asyncquerytask extends asynctask <string, void, featureset> {protected featureset doinbackground (string... queryparams) {If (queryparams = NULL | queryparams. length <= 1) return NULL; string url = queryparams [0]; query = new query (); // create a query parameter object string whereclause = queryparams [1]; spatialreference sr = spatialreference. create (102100); query. setgeometry (new envelope (-20147112.9593773, 557305.257274575,-6569564.7196889, 11753184.6153385); // you can specify a query condition for a spatial query. setoutspatialreference (SR); // you can specify the output coordinate system query. setreturngeometry (true); // specifies whether to return the query of the geometric object. setwhere (whereclause); // set the attribute query condition querytask qtask = new querytask (URL); featureset FS = NULL; try {FS = qtask.exe cute (query ); // execute the Query Task} catch (exception e) {// todo auto-generated catch blocke. printstacktrace (); Return FS;} return FS ;}}}
Through the above code, we can clearly understand that querytask query tasks are very simple to use. The steps are as follows:
1) create a query parameter object
2) Set query conditions for parameter objects
3) use the asynctask subclass to execute the query task.