查詢檢索在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_LAYERS、VISIBLE_LAYERS和TOP_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的具體用法:
params = new IdentifyParameters();//識別任務所需參數對象params.setTolerance(20);//設定容差params.setDPI(98);//設定地圖的DPIparams.setLayers(new int[]{4});//設定要識別的圖層數組params.setLayerMode(IdentifyParameters.ALL_LAYERS);//設定識別模式//為地圖添加點擊事件監聽器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);//設定識別位置params.setSpatialReference(map.getSpatialReference());//設定座標系params.setMapHeight(map.getHeight());//設定地映像素高params.setMapWidth(map.getWidth());//設定地映像素寬Envelope env = new Envelope();map.getExtent().queryEnvelope(env);params.setMapExtent(env);//設定當前地圖範圍MyIdentifyTask mTask = new MyIdentifyTask(identifyPoint);mTask.execute(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.execute(mParams);//執行識別任務} 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");//}}
通過上面代碼我們可以知道,在執行識別任務我們需要以下幾個步驟:
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的使用方法:
targetServerURL = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer";String targetLayer = targetServerURL.concat("/3");//服務圖層String[] queryParams = { targetLayer, "AVGHHSZ_CY>3.5" };AsyncQueryTask ayncQuery = new AsyncQueryTask();ayncQuery.execute(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 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);//設定屬性查詢條件 QueryTask qTask = new QueryTask(url);FeatureSet fs = null;try {fs = qTask.execute(query);//執行查詢任務} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return fs;}return fs;}}}
通過上面代碼我們可以清晰的瞭解的QueryTask查詢任務使用起來非常簡單,步驟如下:
1) 建立Query參數對象
2) 為參數對象設定查詢條件
3) 通過AsyncTask的子類來執行查詢任務