ArcGIS FOR Android 100.3.0:繪製點,線,面,圓,添加文本和圖片

空間要素(Geometry)

Geometries用以在特定地理位置上通過形狀來表達真實世界的對象。圖層範圍、視圖範圍、GPS定位都是通過Geometries表達實現進一步的數據編輯、空間分析、地理處理、位置與面積量算都離不開空間要素。

案例

效果圖: 

佈局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GraphicsOverlayActivity">

    <com.esri.arcgisruntime.mapping.view.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.esri.arcgisruntime.mapping.view.MapView>


    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_draw_point"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="繪製點" />

        <RadioButton
            android:id="@+id/rb_draw_polyline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="繪製線" />

        <RadioButton
            android:id="@+id/rb_draw_scroll_line"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="繪製曲線" />

        <RadioButton
            android:id="@+id/rb_draw_polygon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="繪製面" />

        <RadioButton
            android:id="@+id/rb_add_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加圖片" />

        <RadioButton
            android:id="@+id/rb_draw_circle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="繪製圓" />

        <RadioButton
            android:id="@+id/rb_draw_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="繪製文字" />
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/radiogroup"
        android:onClick="clear"
        android:text="清除" />
</RelativeLayout>

代碼:

public class GraphicsOverlayActivity extends AppCompatActivity {

    private MapView mMapView;

    private RadioGroup mRadioGroup;

    private GraphicsOverlay mGraphicsOverlay;

    //點集合
    private PointCollection mPointCollection = new PointCollection(SpatialReferences.getWebMercator());

    private List<Point> mPointList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graphics_overlay);

        findViews();

        addBasemap();

        initListener();
    }

    private void addBasemap() {
        ArcGISMap arcGISMap = new ArcGISMap(Basemap.Type.OCEANS, 56.075844, -2.681572, 11);
        mMapView.setMap(arcGISMap);


        mGraphicsOverlay = new GraphicsOverlay();
        mMapView.getGraphicsOverlays().add(mGraphicsOverlay);
    }

    private void initListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_draw_point: //繪製點
                        drawPoint();
                        break;
                    case R.id.rb_draw_polyline: //繪製線
                        drawPolyline();
                        break;
                    case R.id.rb_draw_scroll_line: //繪製曲線
                        drawScrollline();
                        break;
                    case R.id.rb_draw_polygon: //繪製面
                        drawPolygon();
                        break;
                    case R.id.rb_add_image: //添加圖片
                        addImage();
                        break;
                    case R.id.rb_draw_circle: //繪製圓
                        drawCircle();
                        break;
                    case R.id.rb_draw_text: //繪製文字
                        drawText();
                        break;
                }
            }
        });
    }

    /**
     * 繪製點
     */
    private void drawPoint() {
        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                Point clickPoint = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
                SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 20);
                Graphic graphic = new Graphic(clickPoint, simpleMarkerSymbol);
                //清除上一個點
                mGraphicsOverlay.getGraphics().clear();
                mGraphicsOverlay.getGraphics().add(graphic);

                //使用渲染器
                //                Graphic graphic1 = new Graphic(clickPoint);
                //                SimpleRenderer simpleRenderer = new SimpleRenderer(simpleMarkerSymbol);
                //                mGraphicsOverlay.setRenderer(simpleRenderer);
                //                mGraphicsOverlay.getGraphics().clear();
                //                mGraphicsOverlay.getGraphics().add(graphic1);

                return super.onSingleTapConfirmed(e);
            }
        });
    }

    /**
     * 繪製線
     */
    private void drawPolyline() {
        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                Point point = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
                mPointCollection.add(point);

                Polyline polyline = new Polyline(mPointCollection);

                //點
                SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10);
                Graphic pointGraphic = new Graphic(point, simpleMarkerSymbol);
                mGraphicsOverlay.getGraphics().add(pointGraphic);

                //線
                SimpleLineSymbol simpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.parseColor("#FC8145"), 3);
                Graphic graphic = new Graphic(polyline, simpleLineSymbol);
                mGraphicsOverlay.getGraphics().add(graphic);

                return super.onSingleTapConfirmed(e);
            }
        });
    }

    /**
     * 繪製曲線
     */
    private void drawScrollline() {
        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Point point1 = mMapView.screenToLocation(new android.graphics.Point(Math.round(e1.getX()), Math.round(e1.getY())));
                Point point2 = mMapView.screenToLocation(new android.graphics.Point(Math.round(e2.getX()), Math.round(e2.getY())));

                mPointCollection.add(point1);
                mPointCollection.add(point2);
                Polyline polyline = new Polyline(mPointCollection);

                Graphic graphic = new Graphic(polyline, new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.parseColor("#FC8145"), 3));
                mGraphicsOverlay.getGraphics().add(graphic);
                return true;
            }
        });
    }

    /**
     * 繪製面
     */
    private void drawPolygon() {

        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {

                mGraphicsOverlay.getGraphics().clear();
                Point point = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
                mPointCollection.add(point);

                Polygon polygon = new Polygon(mPointCollection);

                if (mPointCollection.size() == 1) {
                    SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10);
                    Graphic pointGraphic = new Graphic(point, simpleMarkerSymbol);
                    mGraphicsOverlay.getGraphics().add(pointGraphic);
                }

                SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.GREEN, 3.0f);
                SimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#33e97676"), lineSymbol);
                Graphic graphic = new Graphic(polygon, simpleFillSymbol);
                mGraphicsOverlay.getGraphics().add(graphic);

                return super.onSingleTapConfirmed(e);
            }
        });
    }

    /**
     * 添加圖片
     */
    private void addImage() {
        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {

                Point clickPoint = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                PictureMarkerSymbol pictureMarkerSymbol = new PictureMarkerSymbol(bitmapDrawable);
                final Graphic graphic = new Graphic(clickPoint, pictureMarkerSymbol);

                //涉及到加載圖片到符號裏,所以需要一個異步監聽操作
                pictureMarkerSymbol.loadAsync();
                pictureMarkerSymbol.addDoneLoadingListener(new Runnable() {
                    @Override
                    public void run() {
                        mGraphicsOverlay.getGraphics().clear();
                        mGraphicsOverlay.getGraphics().add(graphic);
                    }
                });

                return super.onSingleTapConfirmed(e);
            }
        });
    }

    /**
     * 繪製文字
     */
    private void drawText() {
        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {

                Point clickPoint = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
                TextSymbol textSymbol = new TextSymbol(20, "繪製文字", Color.RED,
                        TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.MIDDLE);
                Graphic graphic = new Graphic(clickPoint, textSymbol);
                mGraphicsOverlay.getGraphics().clear();
                mGraphicsOverlay.getGraphics().add(graphic);

                return super.onSingleTapConfirmed(e);
            }
        });
    }

    /**
     * 繪製圓
     */
    private void drawCircle() {

        mPointList = new ArrayList<>();

        mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {

                double radius = 0;

                Point point = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));

                mPointList.add(point);
                if (mPointList.size() == 2) {
                    double x = (mPointList.get(1).getX() - mPointList.get(0).getX());
                    double y = (mPointList.get(1).getY() - mPointList.get(0).getY());
                    radius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
                }

                getCircle(mPointList.get(0), radius);

                return super.onSingleTapConfirmed(e);
            }
        });
    }


    private void getCircle(Point point, double radius) {
        //        polygon.setEmpty();
        Point[] points = getPoints(point, radius);
        mPointCollection.clear();
        for (Point p : points) {
            mPointCollection.add(p);
        }

        Polygon polygon = new Polygon(mPointCollection);

        SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10);
        Graphic pointGraphic = new Graphic(point, simpleMarkerSymbol);
        mGraphicsOverlay.getGraphics().add(pointGraphic);

        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.parseColor("#FC8145"), 3.0f);
        SimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.parseColor("#33e97676"), lineSymbol);

        Graphic graphic = new Graphic(polygon, simpleFillSymbol);

        mGraphicsOverlay.getGraphics().add(graphic);
    }

    /**
     * 通過中心點和半徑計算得出圓形的邊線點集合
     *
     * @param center
     * @param radius
     * @return
     */
    private static Point[] getPoints(Point center, double radius) {
        Point[] points = new Point[50];
        double sin;
        double cos;
        double x;
        double y;
        for (double i = 0; i < 50; i++) {
            sin = Math.sin(Math.PI * 2 * i / 50);
            cos = Math.cos(Math.PI * 2 * i / 50);
            x = center.getX() + radius * sin;
            y = center.getY() + radius * cos;
            points[(int) i] = new Point(x, y);
        }
        return points;
    }


    /**
     * 清除
     *
     * @param view
     */
    public void clear(View view) {
        mGraphicsOverlay.getGraphics().clear();
        mPointCollection.clear();
        if (mPointList != null) {
            mPointList.clear();
        }
    }

    private void findViews() {
        mMapView = (MapView) findViewById(R.id.mapview);
        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章