/** * 空间查询:点击某点,红色高亮显示该点所在区域(未成功!无法同步发布查询?)异步查询成功 */ Button sQuery = (Button) findViewById(R.id.squery); sQuery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (locationDisplayManager != null) { locationDisplayManager.stop(); } map.setOnSingleTapListener(new OnSingleTapListener() { @Override public void onSingleTap(float x, float y) { Point point = map.toMapPoint(x,y); Double[] queryArray = {point.getX(),point.getY()}; AsynQueryTask1 asynQueryTask1 = new AsynQueryTask1(); asynQueryTask1.execute(queryArray); gLayer2 = new GraphicsLayer(); gLayer2.setRenderer(new SimpleRenderer(new SimpleFillSymbol(Color.BLACK))); map.addLayer(gLayer2); } });
private class AsynQueryTask1 extends AsyncTask<Double, Void, FeatureResult>{ @Override protected FeatureResult doInBackground(Double... params) { double x =params[0]; double y = params[1]; Point point = new Point(x,y); QueryParameters query = new QueryParameters(); query.setGeometry(point); query.setReturnGeometry(true); query.setSpatialRelationship(SpatialRelationship.INTERSECTS); FeatureResult fs = null; QueryTask queryTask = new QueryTask("http://services.arcgisonline.com/arcgis/rest/services/" + "Demographics/USA_Average_Household_Size/MapServer/3"); try{ fs = queryTask.execute(query); return fs; }catch (Exception e){ e.printStackTrace(); } return null; } @Override protected void onPostExecute(FeatureResult fs) { if(fs != null) { for (Object element : fs) { Feature feature = (Feature) element; gLayer2.clearSelection(); Graphic graphic = new Graphic(feature.getGeometry(), feature.getSymbol(), feature.getAttributes()); gLayer2.addGraphic(graphic); } }else if(fs == null){ Toast.makeText(MainActivity.this,"No result!",Toast.LENGTH_SHORT).show(); } } }