ArcGIS for Android示例解析之FeatureLayer服务-----SelectFeatures

 

SelectFeatures

在这部分中主要讨论一下FeatureLayer图层的应用,此示例让我们可以初步认识一下FeatureLayer,下面我来看一下代码:

public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

            //实例化一个MapView对象

           map = new MapView(this);

           //初始化map的显示范围

map.setExtent(new Envelope(-10868502.895856911, 4470034.144641369,

                      -10837928.084542884, 4492965.25312689), 0);

           //切片地图地图服务

           ArcGISTiledMapServiceLayer tms = new ArcGISTiledMapServiceLayer(

                      "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");

           //添加服务到map中显示

           map.addLayer(tms);

           //FeatureLayer所需的可选参数对象

           Options o = new Options();

           o.mode = MODE.ONDEMAND;//立即执行

           //定义图层显示的字段

           o.outFields = new String[] { "FIELD_KID", "APPROXACRE", "FIELD_NAME",

                      "STATUS", "PROD_GAS", "PROD_OIL", "ACTIVEPROD", "CUMM_OIL",

                      "MAXOILWELL", "LASTOILPRO", "LASTOILWEL", "LASTODATE",

                      "CUMM_GAS", "MAXGASWELL", "LASTGASPRO", "LASTGASWEL",

                      "LASTGDATE", "AVGDEPTH", "AVGDEPTHSL", "FIELD_TYPE",

                      "FIELD_KIDN" };

            //定义FeatureLayer对象,url指向为服务的一个图层

           fLayer = new ArcGISFeatureLayer(

                       "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/1",

                      o);

           //定义面状要素的填充的样式对象,并给面状要素的边界设置样式

           SimpleFillSymbol fiedldsSelectionSymbol = new SimpleFillSymbol(

                      Color.MAGENTA);

           fiedldsSelectionSymbol

                      .setOutline(new SimpleLineSymbol(Color.YELLOW, 2));

           //将图层选择的要素渲染成上面定义的样式

           fLayer.setSelectionSymbol(fiedldsSelectionSymbol);

           //将图层添加到map中

           map.addLayer(fLayer);

 

           // 实例化一个GraphicsLayer图层对象

           gLayer = new GraphicsLayer();

            //实例化一个样式对象

           sfs = new SimpleFillSymbol(Color.BLACK);

           sfs.setOutline(new SimpleLineSymbol(Color.RED, 2));

           sfs.setAlpha(100);

            //添加图层到map中

           map.addLayer(gLayer);

           setContentView(map);

           //自定义扩展MapOnTouchListener类对象

           MyTouchListener touchListener = new MyTouchListener(this, map);

           map.setOnTouchListener(touchListener);//添加map的触摸事件监听

 

           Toast.makeText(this, "Press down to start let go the stop",

                      Toast.LENGTH_SHORT).show();

 

      }

在onCreate()方法中主要是实例化一个MapView对象,给改对象添加了一个切片服务,又定义了一个Featurelayer服务,设置了该图层中选择的要素显示的样式和所需的Options可选参数对象,将其添加到MapView对象中,最后给MapView对象添加了一个GraphicsLayer图层,并给MapView添加了一个触摸事件监听。可以看出,在这个方法中多是一些初始化的工作,而Featurelayer操作在我们自定义的MyTouchListener类中完成的,看下面代码:

class MyTouchListener extends MapOnTouchListener {

 

           Graphic g;

           // 接触地图上的第一个点

           Point p0 = null;

           int uid = -1;

 

           public MyTouchListener(Context arg0, MapView arg1) {

                 super(arg0, arg1);

           }

 

           public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {

                 //判断uid如果等于-1说明这个点是第一个点之下下面if语句,如果不是-1之下else语句

                 if (uid == -1) { // first time

                      g = new Graphic(null, sfs);

                      p0 = map.toMapPoint(from.getX(), from.getY());//将屏幕坐标转成地理坐标

                      uid = gLayer.addGraphic(g);//将要素添加到图层中

 

                 } else {

                      //当手指在屏幕上移动时获取最后的点并和第一点形成一个封闭的对象(一个矩形框)

                      Point p2 = map.toMapPoint(new Point(to.getX(), to.getY()));

                      Envelope envelope = new Envelope();

                      envelope.merge(p0);

                      envelope.merge(p2);

                      gLayer.updateGraphic(uid, envelope);//更新这个矩形框

                     

                 }

 

                 return true;

 

           }         

           public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {

 

                 if (uid != -1) {

                      g = gLayer.getGraphic(uid);//获取这个矩形框要素

                      if (g!= null && g.getGeometry() != null) {

                            fLayer.clearSelection();//清除以前选择的对象

                            Query q = new Query();//定义一个Query对象用于下面的查询操作

                            // 定义它的相关参数及其条件

                            q.setWhere("PROD_GAS='Yes'");

                            q.setReturnGeometry(true);

                            q.setInSpatialReference(map.getSpatialReference());

                            q.setGeometry(g.getGeometry());

                            q.setSpatialRelationship(SpatialRelationship.INTERSECTS);

                            //执行选择查询操作

                            fLayer.selectFeatures(q, SELECTION_METHOD.NEW, callback);

                      }

                      gLayer.removeAll();

 

                 }

 

                 p0 = null;

                 // Resets it

                 uid = -1;

                 return true;

 

           }

 

      }

通过FeatureLayer我们可以很快的查询出所选的要素,并进行渲染。下面我们梳理一下选择查询的步骤:

 

1、              FeatureLayer图层对象所需的参数的设置,如:自身的url设置,Options对象设置,以及选择的要素渲染的样式设置等。

2、              定义一个Query对象,并且给其设置所需的值,如:设置查询条件、是否返回几何对象、输入的空间参考、空间要素以及查询的空间关系。

3、              执行FeatureLayer对象的selectFeatures()方法。

你可能感兴趣的:(android,String,null,url,query,callback)