/** * 异步查询搜索功能:无法同步查询(可尝试) * 属性查询 */ queryIf = (AutoCompleteTextView) findViewById(R.id.query_text); executeQuery = (Button) findViewById(R.id.query); executeQuery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (locationDisplayManager != null) { locationDisplayManager.stop(); } String url = "http://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Average_Household_Size/MapServer/3"; // map.addLayer(new ArcGISTiledMapServiceLayer(url));//不用显示该地图,查询结果在底图显示 String queryClause = queryIf.getText().toString(); String[] queryArray = {url,queryClause}; AsynQueryTask asynQueryTask = new AsynQueryTask(); asynQueryTask.execute(queryArray); }});
ProgressDialog progress; private class AsynQueryTask extends AsyncTask<String, Void, FeatureResult>{ @Override protected void onPreExecute() { progress = new ProgressDialog(MainActivity.this); progress = ProgressDialog.show(MainActivity.this, "", "Please wait....query task is executing"); } @Override protected FeatureResult doInBackground(String... params) { if(params.length<1 || params == null ) { return null; } String url = params[0]; QueryParameters query = new QueryParameters(); //query.setWhere("NAME like '%" + params[1] + "%'"); query.setWhere("AVGHHSZ_CY>" + params[1] ); query.setReturnGeometry(true); query.setInSpatialReference(SpatialReference.create(102100)); QueryTask queryTask = new QueryTask(url); try{ return queryTask.execute(query); }catch (Exception e){ e.printStackTrace(); } return null; } @Override protected void onPostExecute(FeatureResult results) { String message = "No result comes back"; if(results != null){ for (Object element : results){ Feature feature = (Feature) element; // turn feature into graphic gLayer1 = new GraphicsLayer(); SimpleRenderer sr = new SimpleRenderer(new SimpleFillSymbol(Color.RED)); gLayer1.setRenderer(sr); map.addLayer(gLayer1); Graphic graphic = new Graphic(feature.getGeometry(),feature.getSymbol(),feature.getAttributes()); gLayer1.addGraphic(graphic); } } // update message with results message = String.valueOf(results.featureCount()) + " results have returned from query."; progress.dismiss(); Toast toast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG); toast.show(); } }