Android Arcgis入门(三)、GraphicsLayer添加点、线、面

  • Post author:
  • Post category:其他







GraphicsLayer是图形图层,可以自定义图形添加到地图上。调用GraphicsLayer的addGraphic方法就能添加图形,此方法要一个Graphic对象,此对象的构造方法




Graphic

(Geometry geometry, Symbol symbol),前面第一个是要素,而Geometry的子类有Envelope, MultiPath,


MultiPoint


,


Point


,


Segment





Line


,


Polygon


,


Polyline





我们所常用的有点


Point,线Polyline,面Polygon。而后面一


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




一、GraphicsLayer添加点:






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








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

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







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

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





原码如下,

package com.arcgis.test;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import java.util.ArrayList;
import java.util.List;


public class GraphicActivity extends AppCompatActivity {

    private MapView mMapView;
    private Spinner graphicTypeSpinner;
    private Button clernBtn;
    private ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer;
    private GraphicsLayer graphicsLayer;
    private String mapServerUrl = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
    //点集合
    private List<Point> pointList = new ArrayList<>();

    private Graphic graphic;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graphic);
        initView();
        addLayer();
    }

    private void initView() {
        mMapView = (MapView) findViewById(R.id.map_view);
        //mapview点击事件
        mMapView.setOnSingleTapListener(new OnSingleTapListener() {
            @Override
            public void onSingleTap(float x, float y) {
                handleSingleTap(x, y);
            }
        });

        graphicTypeSpinner = (Spinner) findViewById(R.id.spinner_type);
        graphicTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                pointList.removeAll(pointList);
                graphicsLayer.removeAll();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        clernBtn = (Button) findViewById(R.id.clear_graphic);
        clernBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pointList.removeAll(pointList);
                graphicsLayer.removeAll();
            }
        });


    }


    private void addLayer() {
        arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(mapServerUrl);
        mMapView.addLayer(arcGISTiledMapServiceLayer);

        graphicsLayer = new GraphicsLayer();
        mMapView.addLayer(graphicsLayer);
    }


    private void handleSingleTap(float x, float y) {
        Point point = mMapView.toMapPoint(x, y);
        graphicsLayer.removeAll();
        pointList.add(point);
        String type = graphicTypeSpinner.getSelectedItem().toString().trim();
        switch (type) {
            case "点":
                graphic = new Graphic(point, new SimpleMarkerSymbol(Color.RED,5, SimpleMarkerSymbol.STYLE.CIRCLE));
                graphicsLayer.addGraphic(graphic);
                break;
            case "线":
                Polyline polyline = new Polyline();
                if (pointList.size()>1){
                    for(int i=0;i<pointList.size();i++){
                        if (i==0){
                            polyline.startPath(pointList.get(i));
                        }else{
                            polyline.lineTo(pointList.get(i));
                        }
                    }
                }
                graphic = new Graphic(polyline,new SimpleLineSymbol(Color.RED,3, SimpleLineSymbol.STYLE.SOLID));
                graphicsLayer.addGraphic(graphic);
                break;
            case "面":
                Polygon polygon = new Polygon();
                    for(int i=0;i<pointList.size();i++){
                        if (i==0){
                            polygon.startPath(pointList.get(i));
                        }else{
                            polygon.lineTo(pointList.get(i));
                        }
                    }
                graphic = new Graphic(polygon,new SimpleFillSymbol(Color.GREEN,SimpleFillSymbol.STYLE.SOLID));
                graphicsLayer.addGraphic(graphic);
                break;
        }
    }
}







版权声明:本文为Gary__123456原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。