Android GIS开发系列-- 入门季(4) GraphicsLayer的点击查询要素

上一讲中我们学会了如何在MapView中添加Graphic要素,那么在百度或高德地图中,当我们点击要素时,会显示出相应的详细信息。在GraphicsLayer中也提供了这样的方法。下面我们来学习在GraphicsLayer中如何点击查找要素。 首先在MapView中添加两个Graphic要素。代码如下,注意这里用Graphic(Geometry geometry, Symbol symbol, Map<String, Object> attributes)来实例化Graphic,Map<String, Object> attributes是要素的属性值。详细见以下代码。TILED_WORLD_STREETS_URL为网络图层地址。

  1. private void initLayer() {  
  2.           
  3.         mapView.addLayer(new ArcGISTiledMapServiceLayer(  
  4.                 TILED_WORLD_STREETS_URL));  
  5.           
  6.         graphicsLayer = new GraphicsLayer();  
  7.         mapView.addLayer(graphicsLayer);  
  8.   
  9.         Polygon polygon = new Polygon();  
  10.         polygon.startPath(new Point(1.2575908509778766E7,2879410.9266042486));  
  11.         polygon.lineTo(new Point(1.284360696117901E7,3021972.232083669));  
  12.         polygon.lineTo(new Point(1.2826182801620414E7,2713089.403544925));  
  13.         Map<String, Object> attr1 = new HashMap<>();  
  14.         attr1.put("name", "广州");  
  15.         attr1.put("mark", "广州是南方的城市");  
  16.         Graphic graphic1 = new Graphic(polygon, new SimpleFillSymbol(Color.RED),attr1);  
  17.         graphicsLayer.addGraphic(graphic1);  
  18.           
  19.           
  20.         Polygon polygon2 = new Polygon();  
  21.         polygon2.startPath(new Point(1.3388507951011453E7,3611225.628065273));  
  22.         polygon2.lineTo(new Point(1.3607101952746565E7,3858331.890896268));  
  23.         polygon2.lineTo(new Point(1.3613438010767872E7,3449656.14852193));  
  24.         Map<String, Object> attr2 = new HashMap<>();  
  25.         attr2.put("name", "上海");  
  26.         attr2.put("mark", "上海是中部的城市");  
  27.         Graphic graphic2 = new Graphic(polygon2, new SimpleFillSymbol(Color.GREEN),attr2);  
  28.         graphicsLayer.addGraphic(graphic2);  
  29.           
  30.   
  31.     }

效果图如下:


准备工作完成后,设置MapView的点击事件,

  1. mapView.setOnSingleTapListener(new OnSingleTapListener() {  
  2.               
  3.             @Override  
  4.             public void onSingleTap(float x, float y) {  
  5.                 // TODO Auto-generated method stub  
  6.                 handleSingleTap(x,y);  
  7.             }  
  8.         });

在handleSingleTap方法中来处理查询事件,GraphicsLayer查询要用到getGraphicIDs(float x, float y, int tolerance, int numberOfResults)或者getGraphicIDs(float x, float y, int tolerance)方法,前面两个参数是地图点击时的

x与y的值,tolerance是围绕x与y这个点所查询的范围,numberOfResults是要返回结果的大小。

  1. /**  
  2.  * GraphicsLayer的点击查询  
  3.  * @param x  
  4.  * @param y  
  5.  */  
  6. protected void handleSingleTap(float x, float y) {  
  7.       
  8.     int[] graphicIds = graphicsLayer.getGraphicIDs(x, y, 8);  
  9.     if (graphicIds!=null&&graphicIds.length>0) {  
  10.         for (int i = 0; i graphicIds.length; i++) {  
  11.             Graphic graphic = graphicsLayer.getGraphic(graphicIds[i]);  
  12.             Map<String,Objectattr = graphic.getAttributes();  
  13.             Log.i(TAG, attr.get("name")+"===="+attr.get("mark"));  
  14.         }  
  15.     }  
  16. }这样当我们点击要素时,会打出以下的信息。
原文地址:https://www.cnblogs.com/arxive/p/7751872.html