ArcGIS for Android Runtime100 基本操作(一)——点线面测距离长度和面积

来源:互联网 发布:50而知天命什么意思 编辑:程序博客网 时间:2024/06/02 01:59

前言

本篇实现点要素与点要素之间距离量测,线要素长度量测和面要素以及envelope要素面积量测。
效果图:

实现步骤

(1)布局文件(activity_main.xml)
<?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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.trailpointdemo.MainActivity">    <TextView        android:id="@+id/tv_pointdistance"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="pointdistance" />    <TextView        android:id="@+id/tv_ploylinedistance"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_pointdistance"        android:hint="length" />    <TextView        android:id="@+id/tv_ploygonarea"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_ploylinedistance"        android:hint="area" />    <TextView        android:id="@+id/tv_envenloparea"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_ploygonarea"        android:hint="envenlop" />    <!-- MapView -->    <com.esri.arcgisruntime.mapping.view.MapView        android:id="@+id/mapView"        android:layout_below="@+id/tv_envenloparea"        android:layout_width="fill_parent"        android:layout_height="fill_parent">    </com.esri.arcgisruntime.mapping.view.MapView></RelativeLayout>
(2)主活动代码(MainActivity.java)
public class MainActivity extends AppCompatActivity {    private TextView tv_pointdistance,tv_ploylinedistance,tv_ploygonarea,tv_envenloparea;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_pointdistance = (TextView) findViewById(R.id.tv_pointdistance);        tv_ploylinedistance = (TextView) findViewById(R.id.tv_ploylinedistance);        tv_ploygonarea = (TextView) findViewById(R.id.tv_ploygonarea);        tv_envenloparea = (TextView) findViewById(R.id.tv_envenloparea);        MapView mMapView = (MapView) findViewById(R.id.mapView);        final ArcGISMap mMap = new ArcGISMap(Basemap.createTopographic());        mMapView.setMap(mMap);        // create color and symbols for drawing graphics        SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.TRIANGLE, Color.BLUE, 14);        SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.CROSS, Color.BLUE, null);        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 3);        // add a graphic of point, multipoint, polyline and polygon.        GraphicsOverlay overlay = new GraphicsOverlay();        mMapView.getGraphicsOverlays().add(overlay);        Point point1 = new Point(117.195800, 30, SpatialReferences.getWgs84());        Point point2 = new Point(117, 30, SpatialReferences.getWgs84());        Polyline polyline = createPolyline();        Polygon polygon = createPolygon();        Envelope envelope=createEnvelope();        overlay.getGraphics().add(new Graphic(polygon, fillSymbol));        overlay.getGraphics().add(new Graphic(polyline, lineSymbol));        overlay.getGraphics().add(new Graphic(point1, markerSymbol));        overlay.getGraphics().add(new Graphic(point2, markerSymbol));        // use the envelope to set the map viewpoint        mMapView.setViewpointGeometryAsync(envelope, getResources().getDimension(R.dimen.viewpoint_padding));        distancePoints(point1, point2);        lengthPolyline(polyline);        areaPolygon(polygon);        areaEnvenlop(envelope);    }    /**     * @param p1 轨迹点1     * @param p2 轨迹点2     */    private void distancePoints(Point p1, Point p2) {        LinearUnit linearUnit = new LinearUnit(LinearUnitId.METERS);//距离单位        AngularUnit angularUnit = new AngularUnit(AngularUnitId.DEGREES);//角度单位        double distanceValue = GeometryEngine.distanceGeodetic(p1, p2, linearUnit, angularUnit, GeodeticCurveType.GEODESIC).getDistance();        tv_pointdistance.setText("点长度:"+String.valueOf(distanceValue));    }    /**     * 创建线要素     *     * @return     */    private Polyline createPolyline() {        // create a Polyline from a PointCollection        PointCollection borderCAtoNV = new PointCollection(SpatialReferences.getWgs84());        borderCAtoNV.add(-119.992, 41.989);        borderCAtoNV.add(-119.994, 38.994);        borderCAtoNV.add(-114.620, 35.0);        Polyline polyline = new Polyline(borderCAtoNV);        return polyline;    }    /**     * 创建面要素     *     * @return     */    private Polygon createPolygon() {        // create a Polygon from a PointCollection        PointCollection coloradoCorners = new PointCollection(SpatialReferences.getWgs84());        coloradoCorners.add(-109.048, 40.998);        coloradoCorners.add(-102.047, 40.998);        coloradoCorners.add(-102.037, 36.989);        coloradoCorners.add(-109.048, 36.998);        Polygon polygon = new Polygon(coloradoCorners);        return polygon;    }    /**     * 创建Envelope要素     *     * @return     */    private Envelope createEnvelope() {        // create an Envelope using minimum and maximum x,y coordinates and a SpatialReference        Envelope envelope = new Envelope(-123.0, 33.5, -101.0, 48.0, SpatialReferences.getWgs84());        return envelope;    }    /**     * 计算线要素长度     *长度单位和线要素的空间参考的单位一致     * @param pline     */    private void lengthPolyline(Polyline pline) {        double lengthPolyline=GeometryEngine.length(pline);        tv_ploylinedistance.setText("线要素长度:"+String.valueOf(lengthPolyline));    }    /**     * 计算面要素面积     * 面积单位和面要素的空间参考的单位一致     * @param polygon     */    private void areaPolygon(Polygon polygon) {        double areaPolygon=GeometryEngine.area(polygon);        tv_ploygonarea.setText("面要素面积:"+String.valueOf(areaPolygon));    }    /**计算envelope面积     * 面积单位和面要素的空间参考的单位一致     * @param envelope     */    private void areaEnvenlop(Envelope envelope){        double areaEnvenlop=GeometryEngine.area(envelope);        tv_envenloparea.setText("envenlop要素面积:"+String.valueOf(areaEnvenlop));    }}
其中GeometryEngine类中定义的方法都是静态的,旨在对几何图形(geometries)进行不同操作。此外,量测的单位是和点线面几何要素空间参考的单位一致,可以通过对点线面几何要素的空间参考进行投影转换来满足自己对量测单位的要求。
(注:效果图中两点距离长度值的单位是,线要素长度和线要素空间参考一致是,面要素和envelope面积单位也都是基于它们的空间参考的单位来的,读者基于自身需求在代码中修改,效果图仅供参考,不代表博主真实水平大笑。)

源码下载

链接:http://pan.baidu.com/s/1boZDdRD 密码:h1ta
阅读全文
0 0
原创粉丝点击