ArcGIS Runtime for Android开发教程V2.0(5)基础篇---图层

来源:互联网 发布:自学电脑编程最快多久 编辑:程序博客网 时间:2024/05/21 05:22

原址:http://blog.csdn.net/ArcGIS_Mobile/article/details/8151626


[html] view plaincopy
  1. <com.esri.android.map.MapView  
  2.         android:id="@+id/map"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="fill_parent"  
  5.         url="http://www.arcgis.com/home/item.html?id=2b571d8c079d46b4a14a67df42b1da6f"  
  6.         appId="">  
  7.     </com.esri.android.map.MapView>  


        图层是空间数据的载体,通过图层我们可以把地图数据读取到图层中进行显示,在GIS中图层是很重要的概念,图层只有添加到MapView对象中才可以显示加载的地图数据,在ArcGIS Runtime for Android中有许多种图层,不同的图层有不同的作用,下图是图层的关系图:

 

1、 ArcGISTiledMapServiceLayer

        在ArcGIS Server中我们可以发布多种地图服务,移动端需要有不同的图层来对应这些服务。ArcGISTiledMapServiceLayer图层对应ArcGIS Server服务中的切片服务,由于地图服务是切片式的所以它的优势是加载速度快,用法如下:

[html] view plaincopy
  1. MapView mv = new MapView(this);  
  2. mv.addLayer(new ArcGISTiledMapServiceLayer( "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"));  
  3.  setContentView(mv);  

2、 ArcGISDynamicMapServiceLayer

        ArcGISDynamicMapServiceLayer图层对应ArcGIS Server服务中的动态服务,动态地图服务的地图图像是按照移动设备范围读取的,用法如下:

[html] view plaincopy
  1. MapView mv = new MapView(this);  
  2. mv.addLayer(new ArcGISDynamicMapServiceLayer( "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer"));  
  3.  setContentView(mv);  

 

3、 ArcGISImageServiceLayer

 

        ArcGISImageServiceLayer图层对应ArcGIS Server服务中的影像服务,调用影像服务非常简单,用法如下:

 

[html] view plaincopy
  1. MapView mv = new MapView(this);  
  2.  mv.addLayer(new ArcGISImageServiceLayer(  
  3.  "http://myserver/arcgis/rest/services/MyImage/ImageServer",null));  
  4.  setContentView(mv);  


 

4、 ArcGISFeatureLayer

        ArcGISFeatureLayer图层对应ArcGIS Server服务中的Feature Service服务,只有这样的服务才可以进行在线数据编辑,Feature Service服务在调用时可以设置调用的三种模式,不同的模式返回的数据和效率不同,三种模式分别为:MODE.SNAPSHOT按快照方式返回服务的所有数据,MODE.ONDEMAND按需返回数据,MODE.SELECTION按所选的范围返回数据,用法如下:

[html] view plaincopy
  1. String url = "https://servicesbeta.esri.com/ArcGIS/rest/services/SanJuan/TrailConditions/FeatureServer/0";  
  2. MapView mv = new MapView(this);  
  3.  mv.addLayer(new ArcGISFeatureLayer(url,MODE.SNAPSHOT));//按照快照方式  
  4.  setContentView(mv);  


 

5、 ArcGISLocalTiledLayer

 

       ArcGISLocalTiledLayer是添加离线数据包的图层,该图层目前支持两种格式的离线数据:一个是紧凑型的缓存切片,另一个是打包的tpk格式的数据,对于这两种数据的制作方法可以参照附录“如何制作离线数据附录“章节,图层用法如下:

[html] view plaincopy
  1. MapView mv = new MapView(this);  
  2. ArcGISLocalTiledLayer local = new ArcGISLocalTiledLayer("file:///mnt/sdcard/<CacheName>/Layers");//离线图层  
  3.  mv.addLayer(local);  
  4.  setContentView(mv);  


 

6、 GraphicsLayer

        GraphicsLayer在ArcGIS Runtime for Android中是比较重要的一个图层,也是我们使用最为频繁的一个,GraphicsLayer可以包含一个活多个Graphic对象,所以不管我们是查询出来还是自己标绘的Graphic数据都要通过他来呈现,并且MapView添加图层时不要第一个添加这个图层,MapView加载图层时首先要初始化一些地图参数,而该图层不具备这些参数,用法如下:

[html] view plaincopy
  1. MapView mv = new MapView(this);  
  2. mv.addLayer(new GraphicsLayer());  
  3. setContentView(mv);  

        除了可以呈现Graphic对象外,它还具备了一些其他有用的功能,如要素更新与要素获取等等。

  • 要素更新

       这个功能在以后的开发我们会经常用到,如我们要实时更新一个坐标的位置或者用于标绘时,我们以标绘为例来说一下它的更新用处,在我们在移动设备上想实现标绘时,我们就会用到GraphicsLayer的updateGraphic()方法进行实时更新,这时地图上绘制的几何要素将被不断绘制出来,如:

[html] view plaincopy
  1.                public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {  
  2.     if (startPoint == null) {//判断是否已经存在第一个点  
  3.             graphicsLayer.removeAll();  
  4.             poly = new Polyline();                            
  5.             startPoint = mapView.toMapPoint(from.getX(), from.getY());  
  6.             poly.startPath((float) startPoint.getX(),  
  7.                     (float) startPoint.getY());  
  8.             uid = graphicsLayer.addGraphic(new Graphic(poly,new SimpleLineSymbol(Color.BLUE,5)));  
  9.         }  
  10.         poly.lineTo((float) mapPt.getX(), (float) mapPt.getY());//增加线点  
  11.         graphicsLayer. updateGraphic(uid,poly);//更新数据显示  
  12.         return true;  
  13. }  


 

  • 要素获取

       对于在ArcGIS Runtime for Android中,与其他Web API还有所不同,其他API中Graphic对象是可以设置监听的,而在ArcGIS Runtime for Android中Graphic是不能添加任何监听的,所以当我们在地图上点击一个Graphic对象时就需要其他方式间接的获取这个对象。我们可以通过GraphicsLayer的方法getGraphicIDs(float x, float y, int tolerance)来获取要素,其中x和y是屏幕坐标,tolerance是容差,通过这个方法我们就可以间接的获取所需的Graphic对象,如:

[html] view plaincopy
  1.            public boolean onSingleTap(MotionEvent e) {            
  2. Graphic graphic = new Graphic(mapView.toMapPoint(new Point(e.getX(), e.getY())),new SimpleMarkerSymbol(Color.RED,25,STYLE.CIRCLE));  
  3. return false;  
  4.                 int[] getGraphicIDs(float x,float y,            int tolerance)  
  5. }  


 

7、 BingMapsLayer

       ArcGIS Runtime for Android中也可以添加Bing地图服务,想添加Bing地图首先我们必须注册账户并获取Bing map的App ID,有了这个ID我们就有了使用Bing地图的权限,具体的账户申请和操作步骤我们可以参照以下地址:

地址:https://www.bingmapsportal.com/

详细说明:http://msdn.microsoft.com/en-us/library/ff428642.aspx


 

[html] view plaincopy
  1.      <com.esri.android.map.MapView  
  2.     android:id="@+id/map"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     url="http://www.arcgis.com/home/item.html?id=2b571d8c079d46b4a14a67df42b1da6f"  
  6.     appId="">  
  7. </com.esri.android.map.MapView>  

        将申请的ID填入上面代码appId属性即可正常访问Bing地图服务。

8、 SpatialReference      

       空间参考对象,我们可以通过MapView或图层来获取这个对象,对于每一个地图服务,他都有对应的空间参考系,当我们做查询检索或坐标投影转换时会经常用到该对象,用法如下

[html] view plaincopy
  1.                                  double locy = loc.getLatitude();//纬度  
  2. double locx = loc.getLongitude();//经度  
  3. Point wgspoint = new Point(locx, locy);  
  4.                                  //将经纬度坐标转换成地图坐标系  
  5. Point mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),                                           map.getSpatialReference());  
  6.   
  7. Unit mapUnit = map.getSpatialReference().getUnit();  
  8. double zoomWidth = Unit.convertUnits(  
  9.                 SEARCH_RADIUS,  
  10.                 Unit.create(LinearUnit.Code.MILE_US),  
  11.                 mapUnit);  
  12. Envelope zoomExtent = new Envelope(mapPoint,zoomWidth, zoomWidth);  
  13. map.setExtent(zoomExtent);  



0 0
原创粉丝点击