1. 程式人生 > >Arcgis For Android 查詢功能

Arcgis For Android 查詢功能

Arcgis For Android 有三種常用的查詢方式:IdentifyTask 、FindTask 和 QueryTask。

1、QueryTask

QueryTask是一個進行空間和屬性查詢的功能類,它可以在某個地圖服務的某個子圖層內進行查詢,QueryTask進行查詢的地圖服務並不必須載入到Map中進行顯示。QueryTask的執行需要兩個先決條件:一個是需要查詢的圖層URL(需要到具體的子圖層)、一個是進行查詢的過濾條件。

這裡建立一個AsyncQueryTask 用於查詢:

/**
 * 執行 "根據搜尋條件" 查詢的任務
 * @author Administrator
 */
public class AsyncQueryTask extends AsyncTask<String, Void, FeatureResult> {
    private ProgressDialog progressDialog; //查詢提示dialog
    UserCredentials user_sxq; // Arcgis 使用者憑證
    private Context context;

    public AsyncQueryTask(Context context, ProgressDialog progressDialog, UserCredentials user_sxq) {
        this.context = context;
        this.progressDialog = progressDialog;
        this.user_sxq = user_sxq;
    }

    @Override
    protected void onPreExecute() {
        // 查詢前執行的方法
        progressDialog.show();    // 搜尋中,請耐心等待
    }


    @SuppressWarnings("null")
    @Override
    protected FeatureResult doInBackground(String... params) {
        // 查詢中
        FeatureResult fs = null;
        if(params == null && params.length <= 1){
            return null;
        }
        try {
            String whereClause = params[1];
            QueryParameters queryParameters = new QueryParameters();
            queryParameters.setReturnGeometry(true);
            queryParameters.setWhere(whereClause);
            queryParameters.setOutFields(new String[] { "*" });

            SpatialReference sr;  // 查詢的座標系 -- 圖層服務使用的座標系
            if(Constants.Map_Gis_Type == 0){
                sr = SpatialReference.create(Constants.Map_Gis_Type_Str);
            }else{
                sr = SpatialReference.create(Constants.Map_Gis_Type);
            }

            queryParameters.setOutSpatialReference(sr);
            QueryTask queryTask = new QueryTask(params[0], user_sxq);
            fs = queryTask.execute(queryParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fs;
    }

    @Override
    protected void onPostExecute(FeatureResult result) {
        // 查詢結束後執行的方法  
        //doSomeThing
    }

}

在程式碼中呼叫查詢方法

   /**
     * 根據條件搜尋, 供給RightMenuFragment使用
     * @param condition  查詢過濾條件
     * @param queryUrl   待查詢的圖層url
     */
    public void searchByCondition(String condition, String queryUrl) {
        if (!mMapView.isLoaded())
            return;
        if (!(condition.length() > 0))
            return;
        AsyncQueryTask asyncQueryTask = new AsyncQueryTask(getActivity(), progressDialog, user_sxq);
        LogUtil.d("queryUrl" + queryUrl);
        String[] queryParams = {queryUrl, condition};
        asyncQueryTask.execute(queryParams);
    }
2、FindTask

FindTask允許對地圖中一個或多個圖層的要素進行基於屬性欄位值的查詢(search one or more layers in a map for features with attribute values that match or contain an input value)。FindTask不能進行“空間查詢”,因為FindTask可以對多個圖層進行查詢,所有它的url屬性需要指向所查詢的地圖服務的URL,而不像QueryTask需要指定某個圖層的URL。FindTask查詢 是先通過關鍵字匹配待查欄位查詢出結果,如果有過濾條件,再從結果總過濾出符合條件的結果。

在程式碼中使用:

    List<FindResult> res;
    /**
     * 根據條件搜尋, 供給RightMenuFragment使用,多圖層一起查詢
     * @param condition  // 查詢條件
     * @param searchKey// 查詢關鍵字 (設定查詢引數的時候必須設定)
     * @param fields // 待查詢的欄位名稱 陣列
     * @param layerIds  // 查詢服務的子圖層的id陣列
     * @param findLayer // 待查詢的服務URL
     */
    public void searchFromLayers(String condition ,String searchKey,String[] fields , int[] layerIds, String findLayer {

        if (progressDialog != null && !progressDialog.isShowing()){
            progressDialog.show();
        }

        if (!mMapView.isLoaded())
            return;
        if (!(condition.length() > 0))
            return;

        final FindTask findTask = new FindTask(findLayer);
        final FindParameters findParameters = new FindParameters();
        findParameters.setLayerIds(layerIds);

        if (fields.length == 1){
            findParameters.setReturnGeometry(true); //允許返回幾何圖形
            findParameters.setSearchText(searchKey); // 設定查詢關鍵字--必須設定
            findParameters.setSearchFields(fields); // 設定查詢欄位的名稱
        }else {
            Map<Integer,String> parmMap = new HashMap<>();
            for (int a : layerIds){
                parmMap.put(a,condition);  // 設定每個子圖層的 查詢過濾條件
            }

            findParameters.setLayerDefs(parmMap);
            findParameters.setReturnGeometry(true);
            findParameters.setSearchText(searchKey);
            findParameters.setSearchFields(fields);
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    res = findTask.execute(findParameters);
                    // res就是查詢到的結果 List<FindResult>
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
3、IdentifyTask

IdentifyTask是一個在地圖服務中識別要素(Feature)的功能類。通過IdentifyTask可以搜尋地圖層中與輸入幾何形相交的要素(search the layers in a map for features that intersect an input geometry)。因為也是在多個圖層中查詢,所以Task的URL是動態圖層服務的地址。同樣,返回的要素都可以作為Graphic被新增到地圖的GraphicsLayer上。

//例項化物件,並且給實現初始化相應的值
params = new IdentifyParameters();
params.setTolerance(20);
params.setDPI(98);
params.setLayers(new int[]{4});
params.setLayerMode(IdentifyParameters.ALL_LAYERS);
//從map的一個點選地圖的事件監聽 獲取點選事件並設定查詢引數     
public void onSingleTap(final float x,final float y) {                     
      if(!map.isLoaded()){
              return;
      }
      //establish the identify parameters   // 點選的點
      Point 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);//執行非同步操作並傳遞所需的引數                         
   }
});

自建的IdentifyTask

public class MIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]> {
    Point point;
    String layerIp; // 查詢的圖層(具體到子圖層)
    IdentifyTask identifyTask;

    public MIdentifyTask(Point point, String layerIp) {
        this.point = point;
        this.layerIp = layerIp;
    }

    @Override
    protected void onPreExecute() { // 查詢前執行
        identifyTask = new IdentifyTask(layerIp);
    }

    @Override
    protected IdentifyResult[] doInBackground(IdentifyParameters... params) {
        IdentifyResult[] mResult = null;
        if (params != null && params.length > 0) {
            try {
                mResult = identifyTask.execute(params[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mResult;
    }

    @Override
    protected void onPostExecute(IdentifyResult[] result) { //查詢完成後執行
        if (result == null) {
            IdentifyResult[] r = {};
            EventBus.getDefault().post(r);
        } else {
            EventBus.getDefault().post(result);
        }
    }
}

三種查詢的返回結果:

QueryTask:返回的是一個FeatureSet。Featureset.features[i]可以加入到GraphicsLayer上顯示,也可以通過Attributes屬性欄位得到屬性資訊。

FindTask:返回的是一個FindResults陣列, FindResults[i].feature可以加入到GraphicsLayer上顯示,也可以通過Attributes屬性欄位得到屬性資訊。

IdentifyTask:返回的是一個IdentifyResults陣列,IdentifyResults[i].feature可以加入到GraphicsLayer上顯示,也可以通過Attributes屬性欄位得到屬性資訊。

相關推薦

Arcgis For Android 查詢功能

Arcgis For Android 有三種常用的查詢方式:IdentifyTask 、FindTask 和 QueryTask。 1、QueryTask QueryTask是一個進行空間和屬性查詢的功能類,它可以在某個地圖服務的某個子圖層內進行查詢,QueryTask進行查詢的地圖服務並不必須載入到Map中

Arcgis for android 離線查詢

package com.esri.arcgis.android.samples.offlineroutingandgeocoding; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Lis

arcgis for android 實現繪圖功能

一:實現繪圖功能的思路1:首先需要一個點選地圖的一個監聽函式,可以實現點選地圖畫點、線、面。arcgis 提供一個MapOnTouchListener類。2:建立點、線、面對象,arcgis提供的類有Point,Polygon,Line3:設定樣式,不管是點、線、面都有自己的

arcgis for android空間查詢 點選某點,選中該點所在區域,高亮顯示

修改mail.xml <com.esri.android.map.MapView android:id="@+id/map" android:layout_width="fill_parent" an

arcgis for android 離線切片載入與geodatabase查詢

載入geodatabase資料private FeatureLayer valueFL; //離線資料圖 @Override public void getLocalMapServerDiFangData(){ try {

arcgis for android 短路徑分析 查詢最短路徑

ArcGIS for Android(10.1.1)只支援線上的網路分析,執行路徑分析可以通過RoutingTask類的solve方法來進行,通過給slove方法傳遞RoutingParameters型別的引數,可以最短路徑的查詢。而要成功執行路徑分析,就必須釋出網路分析

Arcgis for Android 空間數據WKT與JSON描述

nbsp gis str ext 10.9 select exception json字符串 spatial 點線面數據標準格式 一、 點 WKT: POINT(-118.4 -45.2) JSON: { "x": -118.4, "y": -45.2, "

Arcgis for android的離線數據庫小記

pan eat 下載失敗 roi 一模一樣 數據 ID 找到 code 最近在使用arcgis for android,要實現本地數據的編輯,esri的解決方案搞的太復雜,遇到了不少問題,特此記錄 1.Desktop生成的runtime geodatabase無法在本地進

arcgis for android 呼叫公網天地圖註記重影問題

      最近在android專案開發中,使用arcgis for android 地圖api呼叫公網的天地圖。有一個問題,天地圖的註記層,在地圖縮放的時候重新整理滯後,導致註記層有重影,使用者體驗差。    &nb

ArcGIS for Android 100.3的學習與應用(三) 實現地圖新增自定義指北針

圖為高德地圖實現指北針的效果,那麼ArcGIS如何實現呢? 實現方式: 新增地圖的旋轉監聽: map.addMapRotationChangedListener(new MapRotationChangedListener() { @Override

ArcGIS for Android 100.3的學習與應用(二) 如何移除指定的點和線?

在地圖上新增點和線的時候,我們有時候會遇到要移除或者切換指定的點和線的操作。那麼如何移除指定的點和線呢? ArcGIS的api裡點和線都是由GraphicsOverlay類來進行建立新增的。通過Graphic物件將點或者線的圖形物件(SimpleMarkerSymbol,SimpleLine

ArcGIS for Android 100.3的學習與應用(一) 如何繪製點和線?

平時工作中,我們接觸到的地圖類開發平臺有很多,最常用的有高德,百度,騰訊地圖。而且它們都有自己的開發者平臺和文件供我們使用。基本能滿足我們的業務需求。 由於公司裡的專案會涉及一些地圖資料統計和展示方面的需求,同時也會發布一些地圖服務,所以選擇了使用在地圖方面比較牛逼的ArcGIS。 把平時遇

ArcGIS for Android載入WMS(1)——WMS基本介紹

今天開始寫ArcGIS for Android載入WMS系列的一些記錄,主要包括WMS的基本介紹,ArcGIS for Android載入WMS,用MapGuide和GeoServer釋出WMS並在ArcGIS for Android中載入以及其中存在的一些問題等。因為前兩天

ArcGIS for Android 的學習與應用(二) 如何移除指定的點和線?

在地圖上新增點和線的時候,我們有時候會遇到要移除或者切換指定的點和線的操作。那麼如何移除指定的點和線呢? ArcGIS的api裡點和線都是由GraphicsOverlay類來進行建立新增的。通過Graphic物件將點或者線的圖形物件(SimpleMarkerSy

ArcGIS for Android示例解析之地圖旋轉-----MapRotation

  MapRotation 新增地圖控制元件,並且為地圖容器中新增地圖服務上面已經介紹完了,下面我們來個複雜一點的,這個例子主要實現了地圖的旋轉功能,下面來看一下應用的佈局檔案main.xml,內容如下: <com.esri.android.map.MapView

ArcGIS for Android示例解析之高亮要素-----HighlightFeatures

  HighlightFeatures 要素高亮化功能,相信有其他gis開發經營的開發人員都有過相應的實現經驗,對於高亮要素,簡單說起來就是我們查詢的或識別出來的要素進行渲染,讓其突出顯示而已,這個例子中涉及後面要介紹的識別的內容,我們只簡單介紹相關的知識,主要介紹要素物件

arcgis for android 地圖上畫框劃線

來源:http://blog.csdn.net/mwthe/article/details/49780161 主類中: 完成各種view初始化後,新增該程式碼  MyTouchListener  myListener = new MyTouchListener(contex

ArcGIS for Android Runtime100 基本操作(一)——圖層載入和切換

      之前我打算通過程式碼移植的方式來學習ArcGIS for Android Runtime100,博文地址是:http://blog.csdn.net/bit_kaki/article/details/7719648。但是覺得效果不太理想,因為這個版本的變化實在太

arcgis for android之載入移動地圖包

1、資料製作篇 a.向量資料在arcmap中按需求製作,所需資料放在一個.mxd工程檔案中,或者所需資料存放在一個.geodatabase中; b.arcgis pro 1.3中載入.mxd工程檔案或者.geodatabase檔案,載入之後對圖層中的資料進行相應符號化以及

Arcgis For Android之離線地圖實現的幾種方式

ArcGIS for Android離線資料編輯實現原理 實現ArcGIS for Android上的離線資料編輯,具體實現環境及其步驟如下: 一、      環境準備 1.        軟體環境 1)        ArcGIS Server10用於釋出地圖服務 2)        ArcGIS Des