ArcGIS for Android示例解析之高亮要素-----HighlightFeatures
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()
這樣我們的高亮功能示例就介紹完成了,要想實現不同的、五彩繽紛的效果那就需要我們深入瞭解要素的渲染類及其相關的特性。