移动GIS开发之输入经纬度在地图上画点线面

基于ArGIS API for Android在Esri底图的基础上实现输入经纬度坐标在指定位置做标记,以画点为例,line、polyline、polygon均类似,可参考Esri官网API。

温馨提醒:Esri有的底图是加偏了的,所以最后显示点的位置可能会有不小的偏差,建议选择合适的底图。

目录

一、 新建地图控件

二、 后台代码逻辑

(1)定义变量并与页面控件绑定

(2)新建GraphicsOverlay,并添加至mapView

(3)封装绘制函数

(4)添加两个按钮的监听事件

三、点击运行查看效果


一、 新建地图控件

在地图设计页面上新建两个EditText、两个Button,用于输入经纬度和在地图上显示点与清除点。

    <!--输入经度-->
    <EditText
        android:id="@+id/editText_longitude"
        android:hint="@string/longitude"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:inputType="number"
        android:text="114"
        android:visibility="visible" />
    <!--输入纬度-->
    <EditText
        android:id="@+id/editText_latitude"
        android:hint="@string/latitude"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:layout_marginTop="50dp"
        android:background="@color/colorPrimary"
        android:inputType="number"
        android:text="56.065"
        android:visibility="visible" />
    <!--显示已输入经纬度点的button-->
    <Button
        android:id="@+id/drawBT"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="60dp"
        android:background="@color/colorAccent"
        android:text="@string/xianshi"
        />
    <!--清空已显示点的button-->
    <Button
        android:id="@+id/clearBT"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="120dp"
        android:background="@color/colorAccent"
        android:text="@string/qingkong"
        />

二、 后台代码逻辑

(1)定义变量并与页面控件绑定

        private Button drawBT, clearBT;
        private EditText et_longitude;
        private EditText et_latitude;
        et_longitude = findViewById(R.id.editText_longitude);
        et_latitude = findViewById(R.id.editText_latitude);
        drawBT = findViewById(R.id.drawBT);
        clearBT = findViewById(R.id.clearBT);

(2)新建GraphicsOverlay,并添加至mapView

        // create a graphics overlay
        graphicsOverlay = new GraphicsOverlay();
        // add graphics overlay to the map view
        mMapView.getGraphicsOverlays().add(graphicsOverlay);

(3)封装绘制函数

    //画点
    public void DrawPoint(double x,double y){
        //定义空间参考WGS84
        final SpatialReference SPATIAL_REFERENCE = SpatialReferences.getWgs84();
        //初始化一个新的点叫buoy1Loc
        Point buoy1Loc = new Point(x, y, SPATIAL_REFERENCE);
        //创建一个新的标记符号,0xFFFF0000为红色
        SimpleMarkerSymbol redCircleSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 50);
        //将点buoy1Loc和符号redCircleSymbol绑定,并添加进绘画层graphicsOverlay中
        Graphic buoyGraphic1 = new Graphic(buoy1Loc, redCircleSymbol);
        graphicsOverlay.getGraphics().addAll(Arrays.asList(buoyGraphic1));
    }

        //手动写坐标画折线
    public void createPolylineGraphics() {
        final SpatialReference SPATIAL_REFERENCE = SpatialReferences.getWgs84();
        PointCollection polylinePoints = new PointCollection(SpatialReferences.getWgs84());
        //在此输入折线转折点的坐标
        polylinePoints.add(new Point(113, 50));//第一个点
        polylinePoints.add(new Point(114, 50));//第二个点
        Polyline polyline = new Polyline(polylinePoints);
        SimpleLineSymbol polylineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 50);
        Graphic polylineGraphic = new Graphic(polyline, polylineSymbol);
        graphicsOverlay.getGraphics().addAll(Arrays.asList(polylineGraphic));
    }

(4)添加两个按钮的监听事件

        //画点按钮的监听事件
        drawBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框中的值
                x = valueOf(et_longitude.getText().toString());
                y = valueOf(et_latitude.getText().toString());
                //调用封装好的画点的函数
                DrawPoint(x,y);
            }
        });
        //清空按钮的监听事件
        clearBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                graphicsOverlay.getGraphics().clear();
                longitudelist.clear();
                latitudelist.clear();
            }
        });

三、点击运行查看效果

猜你喜欢

转载自blog.csdn.net/qq_36017609/article/details/85688401