Android GIS开发系列-- 入门季(3) GraphicsLayer添加点、线、面

GraphicsLayer是图形图层,可以自定义图形添加到地图上。调用GraphicsLayer的addGraphic方法就能添加图形,此方法要一个Graphic对象,此对象的构造方法Graphic(Geometry geometry, Symbol symbol),前面第一个是要素,而Geometry的子类有Envelope, MultiPath, MultiPointPointSegmentLinePolygonPolyline我们所常用的有点Point,线Polyline,面Polygon。而后面一

个参数是样式,点的样式是SimpleMarkerSymbol,线的样式是SimpleLineSymbol,面的样式是SimpleFillSymbol。

一、GraphicsLayer添加点:

 

  1. Graphic graphic = new Graphic(point, new SimpleMarkerSymbol(Color.RED,5, SimpleMarkerSymbol.STYLE.CIRCLE));  
  2.  graphicsLayer.addGraphic(graphic);

二、添加线,样式有实线、虚线、点虚线、点点虚线、点线等。效果图如下:

 

  1. Polyline polyline = new Polyline();  
  2.         polyline.startPath(new Point(113,23));//第一个点用startPath,后面的点用lineTo方法  
  3.          polyline.lineTo(new Point(123,23));  
  4.          graphic = new Graphic(polyline,new SimpleLineSymbol(Color.RED,3, SimpleLineSymbol.STYLE.SOLID));  
  5.          graphicsLayer.addGraphic(graphic);

三、添加面Polygon,效果图如下:

 

  1. Polygon polygon = new Polygon();  
  2.                  //第一个点startPath,后面的点用lineTo  
  3.                   polygon.startPath(new Point(118,23));  
  4.                   polygon.lineTo(new Point(118,15));     
  5.                   polygon.lineTo(new Point(113,13));  
  6.                        
  7.                 graphic = new Graphic(polygon,new SimpleFillSymbol(Color.GREEN,SimpleFillSymbol.STYLE.SOLID));  
  8.                 graphicsLayer.addGraphic(graphic);


   

Code for all:

 

  1. package com.arcgis.test;  
  2.   
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5. import android.support.annotation.Nullable;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.Button;  
  10. import android.widget.Spinner;  
  11. import com.esri.android.map.GraphicsLayer;  
  12. import com.esri.android.map.MapView;  
  13. import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;  
  14. import com.esri.android.map.event.OnSingleTapListener;  
  15. import com.esri.core.geometry.Point;  
  16. import com.esri.core.geometry.Polygon;  
  17. import com.esri.core.geometry.Polyline;  
  18. import com.esri.core.map.Graphic;  
  19. import com.esri.core.symbol.SimpleFillSymbol;  
  20. import com.esri.core.symbol.SimpleLineSymbol;  
  21. import com.esri.core.symbol.SimpleMarkerSymbol;  
  22. import java.util.ArrayList;  
  23. import java.util.List;  
  24.   
  25.   
  26. public class GraphicActivity extends AppCompatActivity {  
  27.   
  28.     private MapView mMapView;  
  29.     private Spinner graphicTypeSpinner;  
  30.     private Button clernBtn;  
  31.     private ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer;  
  32.     private GraphicsLayer graphicsLayer;  
  33.     private String mapServerUrl = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";  
  34.     //点集合  
  35.     private List<Point> pointList = new ArrayList<>();  
  36.   
  37.     private Graphic graphic;  
  38.   
  39.     @Override  
  40.     protected void onCreate(@Nullable Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.activity_graphic);  
  43.         initView();  
  44.         addLayer();  
  45.     }  
  46.   
  47.     private void initView() {  
  48.         mMapView = (MapView) findViewById(R.id.map_view);  
  49.         //mapview点击事件  
  50.         mMapView.setOnSingleTapListener(new OnSingleTapListener() {  
  51.             @Override  
  52.             public void onSingleTap(float x, float y) {  
  53.                 handleSingleTap(x, y);  
  54.             }  
  55.         });  
  56.   
  57.         graphicTypeSpinner = (Spinner) findViewById(R.id.spinner_type);  
  58.         graphicTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
  59.             @Override  
  60.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
  61.                 pointList.removeAll(pointList);  
  62.                 graphicsLayer.removeAll();  
  63.             }  
  64.   
  65.             @Override  
  66.             public void onNothingSelected(AdapterView<?> parent) {  
  67.   
  68.             }  
  69.         });  
  70.   
  71.         clernBtn = (Button) findViewById(R.id.clear_graphic);  
  72.         clernBtn.setOnClickListener(new View.OnClickListener() {  
  73.             @Override  
  74.             public void onClick(View v) {  
  75.                 pointList.removeAll(pointList);  
  76.                 graphicsLayer.removeAll();  
  77.             }  
  78.         });  
  79.   
  80.   
  81.     }  
  82.   
  83.   
  84.     private void addLayer() {  
  85.         arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(mapServerUrl);  
  86.         mMapView.addLayer(arcGISTiledMapServiceLayer);  
  87.   
  88.         graphicsLayer = new GraphicsLayer();  
  89.         mMapView.addLayer(graphicsLayer);  
  90.     }  
  91.   
  92.   
  93.     private void handleSingleTap(float x, float y) {  
  94.         Point point = mMapView.toMapPoint(x, y);  
  95.         graphicsLayer.removeAll();  
  96.         pointList.add(point);  
  97.         String type = graphicTypeSpinner.getSelectedItem().toString().trim();  
  98.         switch (type) {  
  99.             case "点":  
  100.                 graphic = new Graphic(point, new SimpleMarkerSymbol(Color.RED,5, SimpleMarkerSymbol.STYLE.CIRCLE));  
  101.                 graphicsLayer.addGraphic(graphic);  
  102.                 break;  
  103.             case "线":  
  104.                 Polyline polyline = new Polyline();  
  105.                 if (pointList.size()>1){  
  106.                     for(int i=0;i<pointList.size();i++){  
  107.                         if (i==0){  
  108.                             polyline.startPath(pointList.get(i));  
  109.                         }else{  
  110.                             polyline.lineTo(pointList.get(i));  
  111.                         }  
  112.                     }  
  113.                 }  
  114.                 graphic = new Graphic(polyline,new SimpleLineSymbol(Color.RED,3, SimpleLineSymbol.STYLE.SOLID));  
  115.                 graphicsLayer.addGraphic(graphic);  
  116.                 break;  
  117.             case "面":  
  118.                 Polygon polygon = new Polygon();  
  119.                     for(int i=0;i<pointList.size();i++){  
  120.                         if (i==0){  
  121.                             polygon.startPath(pointList.get(i));  
  122.                         }else{  
  123.                             polygon.lineTo(pointList.get(i));  
  124.                         }  
  125.                     }  
  126.                 graphic = new Graphic(polygon,new SimpleFillSymbol(Color.GREEN,SimpleFillSymbol.STYLE.SOLID));  
  127.                 graphicsLayer.addGraphic(graphic);  
  128.                 break;  
  129.         }  
  130.     }  

 

posted @ 2017-10-29 23:32  wenglabs  阅读(2959)  评论(0编辑  收藏  举报