ArcGIS Runtime for Android开发教程V2.0(9)基础篇-----查询检索

原址:http://blog.csdn.net/ArcGIS_Mobile/article/details/8263412


  查询检索在ArcGIS Runtime for Android中是不可或缺的一部分,本节将介绍两种常用的查询检索任务:IdentifyTask和QueryTask。IdentifyTask是用来识别图层中的要素的,而QueryTask是用来做图层要素查询的。

1   IdentifyTask

 

 

1.1功能介绍

        IdentifyTask通过字面理解就是一个识别任务类,简单来说就是当我们通过手指点击地图时获取地上的要素信息,当然如果想正常获取要素的相关信息,在识别操作前必须为IdentifyTask事先设置好一组参数信息,IdentifyTask接受的输入参数必须是IdentifyParameters类型的对象,在参数IdentifyParameters对象中我们可以设置相应的识别条件。

        IdentifyTask是针对于服务中的多个图层的识别,返回的结果是IdentifyResult[]数组,并且该任务存在三种模式:

 

l  ALL_LAYERS

该模式表示在识别时检索服务上的所有图层的要素。

l  VISIBLE_LAYERS

该模式表示在识别时只检索服务上的可见图层的要素。

l  TOP_MOST_LAYER

该模式表示在识别时只检索服务上最顶层的要素。

 

        IdentifyParameters常用接口介绍:

序号

接口

说明

1

setDPI(int dpi)

设置map的分辨率值

2

setGeometry(Geometry geometry)

设置空间几何对象

3

setLayerMode(int layerMode)

设置模型,主要有三种模型:ALL_LAYERSVISIBLE_LAYERSTOP_MOST_LAYER

4

setLayers(int[] layers)

设置识别的图层数组

5

setMapExtent(Envelope extent)

设置当前地图的范围

6

setMapHeight(int height)

设置地图的高

7

setMapWidth(int width)

设置地图的宽

8

setReturnGeometry(boolean returnGeometry)

指定是否返回几何对象

9

setSpatialReference(SpatialReference spatialReference)

设置空间参考

10

setTolerance(int tolerance)

设置识别的容差值

1.2 示例

        下面我们通过示例代码来介绍IdentifyTask的具体用法:

[java]  view plain copy
  1. params = new IdentifyParameters();//识别任务所需参数对象  
  2.         params.setTolerance(20);//设置容差  
  3.         params.setDPI(98);//设置地图的DPI  
  4.         params.setLayers(new int[]{4});//设置要识别的图层数组  
  5.         params.setLayerMode(IdentifyParameters.ALL_LAYERS);//设置识别模式  
  6.           
  7.         //为地图添加点击事件监听器  
  8.         map.setOnSingleTapListener(new OnSingleTapListener() {    
  9.             private static final long serialVersionUID = 1L;                      
  10.             public void onSingleTap(final float x, final float y) {           
  11.                 if(!map.isLoaded()){  
  12.                         return;  
  13.                 }  
  14.                     //establish the identify parameters   
  15.                     Point identifyPoint = map.toMapPoint(x, y);               
  16.                     params.setGeometry(identifyPoint);//设置识别位置  
  17.                     params.setSpatialReference(map.getSpatialReference());//设置坐标系                                     
  18.                     params.setMapHeight(map.getHeight());//设置地图像素高  
  19.                     params.setMapWidth(map.getWidth());//设置地图像素宽  
  20.                     Envelope env = new Envelope();  
  21.                     map.getExtent().queryEnvelope(env);  
  22.                     params.setMapExtent(env);//设置当前地图范围                   
  23.                     MyIdentifyTask mTask = new MyIdentifyTask(identifyPoint);  
  24.                     mTask.execute(params);                    
  25.                 }  
  26.   
  27.         });   
  28. ………………………  
  29. private class MyIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]> {  
  30.   
  31.         IdentifyTask mIdentifyTask;  
  32.         Point mAnchor;  
  33.         MyIdentifyTask(Point anchorPoint) {  
  34.             mAnchor = anchorPoint;  
  35.         }  
  36.         @Override  
  37.         protected IdentifyResult[] doInBackground(IdentifyParameters... params) {  
  38.             IdentifyResult[] mResult = null;  
  39.             if (params != null && params.length > 0) {  
  40.                 IdentifyParameters mParams = params[0];  
  41.                 try {  
  42.                     mResult = mIdentifyTask.execute(mParams);//执行识别任务  
  43.                 } catch (Exception e) {  
  44.                     // TODO Auto-generated catch block  
  45.                     e.printStackTrace();  
  46.                 }  
  47.                   
  48.             }  
  49.             return mResult;  
  50.         }  
  51.           
  52.         @Override  
  53.         protected void onPreExecute() {  
  54.             mIdentifyTask = new IdentifyTask("http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer");//  
  55.         }         
  56.     }  


 

通过上面代码我们可以知道,在执行识别任务我们需要以下几个步骤:

1)      创建识别任务所需的参数对象IdentifyParameters

2)      为参数对象设置识别条件

3)      定义MyIdentifyTask类并继承AsyncTask

4)      在MyIdentifyTask的doInBackground()方法中IdentifyTask的execute();

注:在上面示例中,我们的识别任务是在AsyncTask的子类中执行的,因为识别任务请求是一个不定时操作,为了不影响UI中的操作所以使用该类来异步执行识别任务。

 

2 QueryTask

 

 

2.1 功能介绍

        QueryTask指的是一个查询任务,这也是我开发过程中经常使用的一种查询方式,QueryTask查询任务使用非常简单,而且该任务只是针对服务中的一个图层进行查询。在执行QueryTask任务前它需要一个Query参数对象,该参数主要包含了查询的一些条件设置。通过QueryTask我们可以对图层进行属性查询、空间查询以及属性与空间联合查询。

 

Query常用接口介绍:

序号

接口

说明

1

setGeometry(Geometry geometry)

设置空间几何对象

2

setInSpatialReference(SpatialReference inSR)

设置输入的空间参考

3

setObjectIds(int[] objectIds)

设置要查询要素的ObjectID数组

4

setOutFields(String[] outFields)

设置返回字段的数组

5

setOutSpatialReference(SpatialReference outSR)

设置输出的空间参考

6

setReturnGeometry(boolean returnGeometry)

设置是否返回几何对象

7

setReturnIdsOnly(boolean returnIdsOnly)

设置是否只返回ObjiectID字段

8

setSpatialRelationship(SpatialRelationship spatialRelationship)

设置查询的空间关系

9

setWhere(String where)

设置设置查询的条件

2.2 示例

        下面通过示例代码我来看一下QueryTask的使用方法:

[java]  view plain copy
  1. targetServerURL = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer";  
  2. String targetLayer = targetServerURL.concat("/3");//服务图层  
  3.                     String[] queryParams = { targetLayer, "AVGHHSZ_CY>3.5" };  
  4.                     AsyncQueryTask ayncQuery = new AsyncQueryTask();  
  5.                     ayncQuery.execute(queryParams);  
  6. private class AsyncQueryTask extends AsyncTask<String, Void, FeatureSet> {  
  7.         protected FeatureSet doInBackground(String... queryParams) {  
  8.             if (queryParams == null || queryParams.length <= 1)  
  9.                 return null;  
  10.               
  11.             String url = queryParams[0];  
  12.             Query query = new Query();//创建查询参数对象  
  13.             String whereClause = queryParams[1];  
  14.             SpatialReference sr = SpatialReference.create(102100);  
  15.             query.setGeometry(new Envelope(-20147112.9593773557305.257274575,  
  16.                     -6569564.719688911753184.6153385));//设置空间查询条件  
  17.             query.setOutSpatialReference(sr);//设置输出坐标系  
  18.             query.setReturnGeometry(true);//指定是否返回几何对象  
  19.             query.setWhere(whereClause);//设置属性查询条件   
  20.             QueryTask qTask = new QueryTask(url);  
  21.             FeatureSet fs = null;  
  22.             try {  
  23.                 fs = qTask.execute(query);//执行查询任务  
  24.             } catch (Exception e) {  
  25.                 // TODO Auto-generated catch block  
  26.                 e.printStackTrace();  
  27.                 return fs;  
  28.             }  
  29.             return fs;  
  30.         }  
  31.         }  
  32.     }  


 

          通过上面代码我们可以清晰的了解的QueryTask查询任务使用起来非常简单,步骤如下:

1)      创建Query参数对象

2)      为参数对象设定查询条件

3)      通过AsyncTask的子类来执行查询任务

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值