ArcGIS for Android 100.3.0(8):绘制点,线,面,圆,添加文本和图片「建议收藏」

ArcGIS for Android 100.3.0(8):绘制点,线,面,圆,添加文本和图片「建议收藏」空间要素(Geometry)Geometries用以在特定地理位置上通过形状来表达真实世界的对象。图层范围、视图范围、GPS定位都是通过Geometries表达实现进一步的数据编辑、空间分析、地理处理、位置与面积量算都离不开空间要素。案例效果图:布局:<?xmlversion="1.0"encoding="utf-8"?><RelativeL…

大家好,又见面了,我是你们的朋友全栈君。

空间要素(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);
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/160604.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 带你深入了解 GitLab CI/CD 原理及流程

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者:狂乱的贵公子 cnblogs.com/cjsblog/p/12256843.html GitLab CI/CD…

  • jwt解析网站_jwt工作原理

    jwt解析网站_jwt工作原理1.Token与Session优缺点概述1.1Session的由来在登录一个网站进行访问时由于HTTP协议是无状态的就是说一次HTTP请求后他就会被销毁,比如我在www.a.com/login里面登录了,然后你就要访问别的了比如要访问www.a.com/index但是你访问这个网站你就得再发一次HTTP请求,至于说之前的请求跟现在没关,不会有任何记忆,这次访问会失败,因为无法验证你的身份。所以你登录完之后每次在请求上都得带上账号密码等验证身份的信息,但是你天天这么带,那太麻烦了。那还可以这样,把我第一

    2022年10月17日
  • 机器学习之Python — Sklearn库简介

    机器学习之Python — Sklearn库简介文章目录机器学习之Python—Sklearn库简介1Sklearn简介2Sklearn安装3Sklearndatasets4Sklearn通用学习模式5Sklearn数据预处理–标准化6Sklearn交叉验证7总结参考资料机器学习之Python—Sklearn库简介1Sklearn简介Scikit-learn(sklearn)是机器学习中常用的第三方模块,对常…

    2022年10月11日
  • TinyProxy:移动联通电信各个卡定向免流方法教程[通俗易懂]

    下载地址:https://www.lanzoui.com/i0Tsigfiv7c开始以电信星卡为例:打开软件右上角设置下高级模式然后返回复制下这模式【百度直连】免歪卡、星卡、小歪卡、小抖卡、大圣卡、百度系可免listen_port=65080; daemon=on; worker_proc=0; uid=3004; http_ip=112.80.255.21; http_port=443; http_del=”X-Online-Host,Host”..

  • 解决导入MySQL数据库提示”Unknown character set: ‘utf8mb4′”错误

    解决导入MySQL数据库提示”Unknown character set: ‘utf8mb4′”错误

  • leetcode-11盛最多水的容器「建议收藏」

    leetcode-11盛最多水的容器「建议收藏」原题链接给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器。示例 1:输入:[1,8,6,2,5,4,8,3,7]输出:49解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。示例 2:输入:he

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号