ArcGIS for Android Sample parsing spatial query-----querytask

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/wozaifeiyang0/article/details/7331450

Querytask

Query functionality is an essential feature in GIS, and the example provides a demo of a property query, and here's a look at how we implement a query function in Android.

1, we added a button in the layout file, and added this button in the code click event Listener;

2, custom a Asynctask extension class, in this class we implement the query function.

3. Execute our custom asynchronous class in the event listener class of the button.

The relevant code is as follows:

Querybt.setonclicklistener (new View.onclicklistener () {

Public void OnClick (View v) {

if (Blquery) {

String Targetlayer = Targetserverurl.concat ("/3");

String[] Queryparams = {targetlayer, "avghhsz_cy>3.5"};

Asyncquerytask ayncquery = new asyncquerytask ();

Ayncquery.execute (Queryparams);

} Else {

Gl.removeall ();

Blquery = true;

Querybt.settext ("Average household > 3.5");

}

}

});

The above code defines a button's Click event Listener and executes our custom asynchronous class in the listener, which implements the query function in the asynchronous class, so let's take a look at what this class does and the code is as follows:

Private Class Asyncquerytask extends asynctask<string, Void, featureset> {

protected void OnPreExecute () {

Show a progress bar when no results are queried

progress = ProgressDialog. Show (Attributequery. This, "",

"Wait....query task is executing");

}

protected FeatureSet doinbackground (String ... queryparams) {

if (Queryparams = = Null | | queryparams.length <= 1)

return null;

Query criteria and URL parameters

String URL = queryparams[0];

Query the required parameter classes

Query query = new query ();

String whereclause = queryparams[1];

spatialreference sr = spatialreference. Create (102100);

Query.setgeometry (new Envelope (-20147112.9593773, 557305.257274575,

-6569564.7196889, 11753184.6153385));//Set Query space scope

Query.setoutspatialreference (SR);//Set the coordinate system of the query output

Query.setreturngeometry (true);//whether to return spatial information

Query.setwhere (whereclause);//where conditions

Querytask qtask = new querytask (URL);//Query Task class

FeatureSet fs = null;

Try {

FS = Qtask.execute (query);//execute query, return query result

} catch (Exception e) {

TODO auto-generated Catch block

E.printstacktrace ();

return FS;

}

return FS;

}

protected void OnPostExecute (featureset result) {

String message = "No result comes back";

if (Result! = null) {

graphic[] Grs = Result.getgraphics ();

if (Grs.length > 0) {

Gl.addgraphics (GRS); Add query results to a layer

Message = (Grs.length = = 1?) "1 result has": Integer

. toString (grs.length) + "Results has")

+ "Come Back";

}

}

Progress.dismiss ();//Stop progress bar

Toast toast = toast. Maketext (Attributequery. This, message,

Toast. Length_long);

Toast.show ();

Querybt.settext ("Clear graphics");

Blquery = false;

}

}

As you can see through comments and code, it's not a hassle to make a query, just configure two classes: Query and Querytask. Query is the factor to be queried, Qureytask is the executor of the query task, we just need to set the query for the relevant parameters, and give qureytask to specify the URL path of the query, query function can be completed successfully.

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.