|
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);//初始化識別任務執行個體
}
} |