如上图,点击某块,然后高亮显示选中的这块区域(本例中是红色的那个块)。
---------------------------
编码过程:
1.注册 mapView的单击事件
mMapView.setOnSingleTapListener(mOnSingleTapListener);
2.实现单击的时间的监听器
OnSingleTapListener mOnSingleTapListener = new OnSingleTapListener() { @Override public void onSingleTap(float x, float y) { new AsyncQueryTask().execute(x, y); } };
我们具体在一个AsyncQueryTask里异步执行查询和显示。
在上面的onSingleTap方法里有有两个参数 (float x, float y) ,这个是一个坐标点,表示里你点击的位置,是个屏幕坐标,在查询时,我们需要将其转成 地图坐标点,使用方法
mMapView.toMapPoint(x,y)
class AsyncQueryTask extends AsyncTask{ String errMsg = null; @Override protected FeatureSet doInBackground(Float... params) { float x = params[0]; float y = params[1]; if (mMapView.isLoaded()) { // AlertMsg("单击,屏幕像素坐标点: x=%s,y=%s", x, y); // Point mapPoint = mMapView.toMapPoint(new Point(x, y)); Point pt = mMapView.toMapPoint(x, y); Query query = new Query(); query.setGeometry(pt); query.setReturnGeometry(true); //query.setOutFields(new String[] {"*"}); query.setOutFields(new String[] { "县名称_1", "乡名称_1", "村名称_1", "地块名称", "统一编号" }); QueryTask queryTask = new QueryTask(mMapServiceUrl + "/4");//这里参数是地图的服务地址,后面的4,标识了是其中某一个图层。 try { FeatureSet fs = queryTask.execute(query); return fs; } catch (Exception e) { e.printStackTrace(); errMsg = e.getMessage(); } } return null; }
我们构建一个 query 和 queryTask 进行查询,
查询条件: query.setGeometry(pt);
返回的字段内容:query.setOutFields(new String[] { "县名称_1", "乡名称_1", "村名称_1", "地块名称", "统一编号" });
返回的结果是 FeatureSet 就是想要获得区域。
3.从返回的结果中拿到 grphics 并绘制在一个 自定义的图层上 ,并指定绘制方式。
@Override protected void onPostExecute(FeatureSet fs) { if (fs == null) { if (errMsg != null) { Toast.makeText(CT_Area_ViewActivity.this, errMsg, 0).show(); } Toast.makeText(CT_Area_ViewActivity.this, "查询的结果为空", 0).show(); return; } try { Toast.makeText(CT_Area_ViewActivity.this, "查询到" + fs.getGraphics().length + "个结果", 0).show(); SimpleFillSymbol symbol = new SimpleFillSymbol(Color.RED); CT_Area_ViewActivity.this.gLayer .setRenderer(new SimpleRenderer(symbol)); CT_Area_ViewActivity.this.gLayer.removeAll(); CT_Area_ViewActivity.this.gLayer.addGraphics(fs.getGraphics()); if (fs.getGraphics().length > 0) { Graphic graphic = fs.getGraphics()[0]; // String[] names = graphic.getAttributeNames(); String xian = getValue(graphic, "县名称_1", ""); String xiang = getValue(graphic, "乡名称_1", ""); String cun = getValue(graphic, "村名称_1", ""); String dikuai = getValue(graphic, "地块名称", ""); String id = getValue(graphic, "统一编号", ""); ShowDialog_Detail(xian + xiang + cun + "\n" + dikuai, id); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(CT_Area_ViewActivity.this, e.getMessage(), 0) .show(); } } String getValue(Graphic graphic, String key, String defaultVal) { Object obj = graphic.getAttributeValue(key); if (obj == null) return defaultVal; else return obj.toString(); } }
别忘了声明一些 类变量:
com.esri.android.map.MapView mMapView;
ArcGISDynamicMapServiceLayer agmServiceLayer;
GraphicsLayer gLayer;