ArcGIS for Android樣本解析之高亮要素-----HighlightFeatures

來源:互聯網
上載者:User

標籤:android   style   class   blog   http   ext   

轉自:http://blog.csdn.net/wozaifeiyang0/article/details/7323606

HighlightFeatures

要素高亮化功能,相信有其他gis開發經營的開發人員都有過相應的實現經驗,對於高亮要素,簡單說起來就是我們查詢的或識別出來的要素進行渲染,讓其反白而已,這個例子中涉及後面要介紹的識別的內容,我們只簡單介紹相關的知識,主要介紹要素對象的渲染(也就是所謂的高亮),來看代碼:

mapView.setOnLongPressListener(new OnLongPressListener() {

      public void onLongPress(float x,float y) {

        try {

          if (tiledMapServiceLayer.isInitialized() && selectedLayerIndex >= 0) {

             

           graphicsLayer.removeAll();

            /*

             *點擊地圖的點

             */

            Point pointClicked = mapView.toMapPoint(x, y);

 

            /*

             * 識別任務所需的參數,初始化相應的值

             */

            IdentifyParameters inputParameters = new IdentifyParameters();

            inputParameters.setGeometry(pointClicked);

            inputParameters.setLayers(new int[] { layerIndexes[selectedLayerIndex] });

            Envelope env = new Envelope();

                 mapView.getExtent().queryEnvelope(env);

                 inputParameters.setSpatialReference(mapView.getSpatialReference());

                 inputParameters.setMapExtent(env);

            inputParameters.setDPI(96);

            inputParameters.setMapHeight(mapView.getHeight());

            inputParameters.setMapWidth(mapView.getWidth());

            inputParameters.setTolerance(10);

 

            /*

             * 這是我們自己擴充的類,在其中主要實現了IdentifyTask的請求

             */

            MyIdentifyTask mIdenitfy = new MyIdentifyTask();

             //執行非同步作業並將參數傳入非同步作業中

            mIdenitfy.execute(inputParameters);           

 

           

          } else {

            Toast toast = Toast.makeText(getApplicationContext(), "Please select a layer to identify features from.",

                Toast.LENGTH_SHORT);

            toast.show();

          }

        } catch (Exception ex) {

          ex.printStackTrace();

        }

      }

    });

上面的代碼中,主要給地圖添加了一個長按地圖事件監聽,在事件處理函數中主要做了初始化識別任務的參數及其執行我們擴充的MyIdentifyTask操作,MyIdentifyTask其實就是一個非同步請求類,下面我們來看看,這非同步請求類做了什麼,代碼如下:

private class MyIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]> {

 

           IdentifyTask mIdentifyTask;      

           @Override

           protected 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) {                          

                            e.printStackTrace();

                      }

                     

                 }

                 return mResult;

           }

           @Override

           protected void onPostExecute(IdentifyResult[] results) {

                 // TODO Auto-generated method stub

                

                 if (results != null && results.length > 0) {

                    //產生要素對象數組

                    highlightGraphics = new Graphic[results.length];

 

                    Toast toast = Toast.makeText(getApplicationContext(), results.length + " features identified\n",

                        Toast.LENGTH_LONG);

                    toast.setGravity(Gravity.BOTTOM, 0, 0);

                    toast.show();             

                    for (int i = 0; i < results.length; i++) {

                      Geometry geom = results[i].getGeometry();

                      String typeName = geom.getType().name();

                      //在這裡我們進行要素的高亮顯示,也就是要素渲染工作

 

                      Random r = new Random();

                      int color = Color.rgb(r.nextInt(255), r.nextInt(255), r.nextInt(255));

                      if (typeName.equalsIgnoreCase("point")) {

                        SimpleMarkerSymbol sms = new SimpleMarkerSymbol(color, 20, STYLE.SQUARE);

                        highlightGraphics[i] = new Graphic(geom, sms);

                      } else if (typeName.equalsIgnoreCase("polyline")) {

                        SimpleLineSymbol sls = new SimpleLineSymbol(color, 5);

                        highlightGraphics[i] = new Graphic(geom, sls);

                      } else if (typeName.equalsIgnoreCase("polygon")) {

                        SimpleFillSymbol sfs = new SimpleFillSymbol(color);

                        sfs.setAlpha(75);

                        highlightGraphics[i] = new Graphic(geom, sfs);

                      }                 

                      graphicsLayer.addGraphic(highlightGraphics[i]);

                      clearButton.setEnabled(true);

                    }

                  } else {

                    Toast toast = Toast.makeText(getApplicationContext(), "No features identified.", Toast.LENGTH_SHORT);

                    toast.show();

                  }

                

           }

 

           @Override

           protected void onPreExecute() {             

                 mIdentifyTask = new IdentifyTask(mapURL);//初始化識別任務執行個體

           }

      }

在這裡我們可以看到,這個非同步類主要做了執行個體化識別任務對象,並且執行識別任務,返回的結果再進行渲染顯示,對於Android中的非同步類AsyncTask應該有所瞭解吧,簡單介紹一下他的執行過程,當我們產生AsyncTask執行個體並執行execute()方法後,他的內部還是執行順序為onPreExecute()à doInBackground()àonPostExecute()

這樣我們的高亮功能樣本就介紹完成了,要想實現不同的、五彩繽紛的效果那就需要我們深入瞭解要素的渲染類及其相關的特性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.