IdentifyTask--要素识别 ----长按某一点后查询,并将查询结果高亮显示

public class HighlightFeatures extends Activity {

	private MapView mapView;
	private ArcGISTiledMapServiceLayer tiledMapServiceLayer;
	private GraphicsLayer graphicsLayer;
	private Graphic[] highlightGraphics;

	private String mapURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/PublicSafety/PublicSafetyBasemap/MapServer";

	@SuppressWarnings("serial")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.map);
		mapView.setOnLongPressListener(new OnLongPressListener() {
			public void onLongPress(float x, float y) {
				identifyPoint(x, y);
			}
		});

		tiledMapServiceLayer = new ArcGISTiledMapServiceLayer(mapURL);
		graphicsLayer = new GraphicsLayer();

		mapView.addLayer(tiledMapServiceLayer);
		mapView.addLayer(graphicsLayer);
	}

	// 要素识别长按点
	protected void identifyPoint(float x, float y) {
		try {
			graphicsLayer.removeAll();
			Point pointClicked = mapView.toMapPoint(x, y);

			IdentifyParameters params = new IdentifyParameters();
			params.setGeometry(pointClicked);
			params.setLayers(new int[] { 28 });
			Envelope env = new Envelope();
			mapView.getExtent().queryEnvelope(env);
			params.setSpatialReference(mapView.getSpatialReference());
			params.setMapExtent(env);
			params.setDPI(96);
			params.setMapHeight(mapView.getHeight());
			params.setMapWidth(mapView.getWidth());
			params.setTolerance(10);

			// 执行查询任务
			MyIdentifyTask mIdenitfy = new MyIdentifyTask();
			mIdenitfy.execute(params);

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		mapView.pause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mapView.unpause();
	}

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

			if (results != null && results.length > 0) {
				highlightGraphics = new Graphic[results.length];
				for (int i = 0; i < results.length; i++) {

					Geometry geom = results[i].getGeometry();// 得到几何对象
					String typeName = geom.getType().name();// 图层类型
					lightShow(i, typeName, geom);// 高亮显示

					IdentifyResult result = results[i];
					Log.i("andli", "图层名称---" + result.getLayerName());
					Log.i("andli", "图层id ---" + result.getLayerId());
					Log.i("andli", "图层类型---" + typeName);
				}
			} else {
				Toast.makeText(HighlightFeatures.this, "查询结果为空", 3000).show();
			}

		}

		@Override
		protected void onPreExecute() {
			mIdentifyTask = new IdentifyTask(mapURL);
		}

	}

	public void lightShow(int i, String typeName, Geometry geom) {
		// 高亮显示查询结果
		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]);

	}
}
 

你可能感兴趣的:(task)