首页 > 代码库 > ArcGIS for Android示例解析之空间查询-----QueryTask

ArcGIS for Android示例解析之空间查询-----QueryTask

转自:http://blog.csdn.net/wozaifeiyang0/article/details/7331450

QueryTask

查询功能在GIS中是一个不可或缺的重要功能,示例中提供了一个属性查询的Demo,下面来看一下在Android中我们怎样实现一个查询功能,

1、              我们在布局文件中添加了一个按钮,并且在代码中添加了这个按钮的点击事件监听;

2、              自定义一个 AsyncTask的扩展类,在此类中我们实现了查询功能。

3、              在按钮的事件监听类中执行我们自定义的异步类。

相关代码如下:

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");

 

                      }

                 }

           });

上面代码中定义了一个按钮的点击事件监听,并在监听中执行我们自定义的异步类,异步类中实现了查询功能,下面我们来看一下这个类都做了什么,代码如下:

private class AsyncQueryTask extends AsyncTask<String, Void, FeatureSet> {

              

           protected void onPreExecute() {

                 //在未查询出结果时显示一个进度条

                 progress = ProgressDialog.show(AttributeQuery.this, "",

                            "Please wait....query task is executing");

 

           }

           protected FeatureSet doInBackground(String... queryParams) {

                 if (queryParams == null || queryParams.length <= 1)

                      return null;

                 //查询条件和URL参数

                 String url = queryParams[0];

                 //查询所需的参数类

                 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));//设置查询空间范围

                 query.setOutSpatialReference(sr);//设置查询输出的坐标系

                 query.setReturnGeometry(true);//是否返回空间信息

                 query.setWhere(whereClause);//where条件

 

                 QueryTask qTask = new QueryTask(url);//查询任务类

                 FeatureSet fs = null;

 

                 try {

                      fs = qTask.execute(query);//执行查询,返回查询结果

                 } 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);  //将查询结果添加到图层上                    

                            message = (grs.length == 1 ? "1 result has " : Integer

                                       .toString(grs.length) + " results have ")

                                       + "come back";

                      }

 

                 }

                 progress.dismiss();//停止进度条

 

                 Toast toast = Toast.makeText(AttributeQuery.this, message,

                            Toast.LENGTH_LONG);

                 toast.show();

                 querybt.setText("Clear graphics");

                 blQuery = false;

 

           }

 

      }

通过注释和代码可以看出,要做一个查询其实一点都不麻烦,只需要配置好两个类:Query和QueryTask。Query是要查询的因子,QureyTask是查询任务的执行者,我们只需给Query设定好要查询的相关参数,并给QureyTask指定查询的URL路径,查询功能就可以顺利完成。