ArcGIS for Android FindTask的簡單使用

來源:互聯網
上載者:User

在ArcGIS  for Android(10.1.1) 尋找使用FindTask這個類,給它傳入FindParameters的參數,可以執行尋找操作,並返回FindResult類型的List集合,跟QueryTask和IdentifyTask都有點像,但它們三個還是有差別的:

QueryTask所尋找的對象為一個MapServer的只圖層,並可以可以不用載入進來就可以直接進行查詢,可以進行空間查詢和屬性查詢。

IdentifyTask識別的對象為一個MapServer,進行的是空間識別。

FindTask的尋找對象也是一個MapServer,可以根據設定尋找的字串,進行屬性的尋找,

進行的是屬性查詢

 

以下代碼,是在一個MapServer的layerID為2的圖層中,尋找所有欄位指中與“island”相匹配的斑塊並顯示出來。

public class FindTaskTestActivity extends Activity {private List<FindResult> res;private GraphicsLayer gl;MapView mMapView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mMapView = (MapView) findViewById(R.id.map);ArcGISDynamicMapServiceLayer basemap = new ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer");mMapView.addLayer(basemap);mMapView.setExtent(new Envelope(-159.267600772764,11.2206525667169,-32.0310878479893, 62.5140046432286));gl=new GraphicsLayer();mMapView.addLayer(gl);mMapView.setOnSingleTapListener(new MyOnSingleTagLis());}class MyOnSingleTagLis implements OnSingleTapListener {public void onSingleTap(float arg0, float arg1) {//定義各種尋找參數final FindParameters paras = new FindParameters();paras.setReturnGeometry(true);//需要返回Geometryparas.setSearchText("island");//搜尋索引鍵為islandparas.setLayerIds(new int[]{2});//尋找圖層(預設為尋找所有圖層)//paras.setSearchFields(new String[]{});//搜尋自動化,沒有設定預設為搜尋全部欄位final FindTask findtask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer");new Thread() {public void run() {try {//在子線程中執行操作 res=findtask.execute(paras); proResult(res);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();}}/** * 處理尋找結果,把結果顯示出來 */private void proResult(final List<FindResult> res){if(res==null || res.size()==0){return;}runOnUiThread(new Runnable() {public void run() {for(FindResult fr: res){Geometry g=fr.getGeometry();//由於知道我們尋找的圖層是個面圖層,可以定義面的符號,如果不知道圖層的類型,可以通過fr.getGeometryType()判斷SimpleFillSymbol sfs=new SimpleFillSymbol(Color.RED);//把尋找的圖型顯示出來gl.addGraphic(new Graphic(g, sfs));//擷取所有屬性,並遍曆出來Map<String, Object> atts= fr.getAttributes();Set<Entry<String, Object>> entrys= atts.entrySet();for(Entry<String, Object> entry :entrys){Log.i(entry.getKey(), entry.getValue().toString());}Log.i("Attributes", "----------------------------------------");//由於上面的設定尋找的結果只有一個FindResult,可以把地圖縮放到這個尋找的結果範圍,便於觀察//如果有多個返回結果,就不適合這樣弄啦mMapView.setExtent(g,100);}}});}@Overrideprotected void onDestroy() {super.onDestroy();}@Overrideprotected void onPause() {super.onPause();mMapView.pause();}@Overrideprotected void onResume() {super.onResume();mMapView.unpause();}}

結果

下載全部代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.