ArcGIS Android API 應用程式開發之圖形繪製與長度面積量算

來源:互聯網
上載者:User

本文通過一個簡單的小Demo,向大家介紹如何使用ArcGIS Android API繪製點、線、面圖形,並且測量線的長度和多邊形的面積。

首先來看一下程式啟動並執行:

實現上述功能,首先需要向地圖中添加一個GraphicsLayer,用來繪製Point、Polyline、Polygon等Graphic對象。其次需要監聽地圖的Touch事件,即繼承MapOnTouchListener類。最後調用Line或Polyline對象的calculateLength2D方法計算長度,調用Polygon對象的calculateArea2D的方法計算面積。

主要代碼如下:

在main.xml布局檔案中添加MapView控制項:

<com.esri.android.map.MapView    android:id="@+id/map"    android:layout_width="fill_parent"    android:layout_height="fill_parent"       initExtent = "11575115, 3575676, 11596612, 3599415" />

在主Activity類中(本例是MeasureTestActivity.java)定義需要用到的全域變數:

private MapView map;private GraphicsLayer drawLayer;private String strMapUrl="http://www.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_CHN/ChengDu_Community_BaseMap_CHN/MapServer";private SimpleLineSymbol lineSymbol;private SimpleMarkerSymbol markerSymbol;private SimpleFillSymbol fillSymbol;private MapTouchListener mapTouchListener;在MeasureTestActivity類的onCreate方法中擷取地圖,添加圖層:map = (MapView)findViewById(R.id.map);ArcGISTiledMapServiceLayer baseMapLayer = new ArcGISTiledMapServiceLayer(strMapUrl);map.addLayer(baseMapLayer);drawLayer = new GraphicsLayer();map.addLayer(drawLayer);還是在MeasureTestActivity類的onCreate方法中,添加對地圖touch事件的監聽:mapTouchListener = new MapTouchListener(MeasureTestActivity.this, map);map.setOnTouchListener(mapTouchListener);

其中MapTouchListener類繼承自MapOnTouchListener類,主要用於監聽地圖上的touch事件,包括onSingleTap和onDoubleTap事件。onSingleTap用於繪製點、繪製當前線段(並計算長度)、繪製現有點構成的臨時多邊形(並計算當前面積);onDoubleTap用於結束繪製,並計算總長度和總面積。

    class MapTouchListener extends MapOnTouchListener {    private Geometry.Type geoType = null;//用於判定當前選擇的幾何圖形類型    private Point ptStart = null;//起點    private Point ptPrevious = null;//上一個點    private ArrayList<Point> points = null;//記錄全部點    private Polygon tempPolygon = null;//記錄繪製過程中的多邊形    public MapTouchListener(Context context, MapView view) {super(context, view);points = new ArrayList<Point>();}// 根據使用者選擇設定當前繪製的幾何圖形類型public void setType(String geometryType) {if(geometryType.equalsIgnoreCase("Point"))this.geoType = Geometry.Type.Point;else if(geometryType.equalsIgnoreCase("Polyline"))this.geoType = Geometry.Type.Polyline;else if(geometryType.equalsIgnoreCase("Polygon"))this.geoType = Geometry.Type.Polygon;}public Geometry.Type getType() {return this.geoType;}@Overridepublic boolean onSingleTap(MotionEvent point) {Point ptCurrent = map.toMapPoint(new Point(point.getX(), point.getY()));if(ptStart == null) drawLayer.removeAll();//第一次開始前,清空全部graphicif (geoType == Geometry.Type.Point) {//直接畫點drawLayer.removeAll();ptStart = ptCurrent;Graphic graphic = new Graphic(ptStart,markerSymbol);drawLayer.addGraphic(graphic);btnClear.setEnabled(true);return true;}else//繪製線或多邊形{points.add(ptCurrent);//將當前點加入點集合中if(ptStart == null){//畫線或多邊形的第一個點ptStart = ptCurrent;//繪製第一個點Graphic graphic = new Graphic(ptStart,markerSymbol);drawLayer.addGraphic(graphic);}else{//畫線或多邊形的其他點//繪製其他點Graphic graphic = new Graphic(ptCurrent,markerSymbol);drawLayer.addGraphic(graphic);//產生當前線段(由當前點和上一個點構成)Line line = new Line();line.setStart(ptPrevious);line.setEnd(ptCurrent);if(geoType == Geometry.Type.Polyline){//繪製當前線段Polyline polyline = new Polyline();polyline.addSegment(line, true);Graphic g = new Graphic(polyline, lineSymbol);drawLayer.addGraphic(g);// 計算當前線段的長度String length = Double.toString(Math.round(line.calculateLength2D())) + " 米";Toast.makeText(map.getContext(), length, Toast.LENGTH_SHORT).show();}else{//繪製臨時多邊形if(tempPolygon == null) tempPolygon = new Polygon();tempPolygon.addSegment(line, false);drawLayer.removeAll();Graphic g = new Graphic(tempPolygon, fillSymbol);drawLayer.addGraphic(g);//計算當前面積String sArea = getAreaString(tempPolygon.calculateArea2D());Toast.makeText(map.getContext(), sArea, Toast.LENGTH_SHORT).show();}}ptPrevious = ptCurrent;return true;}}@Overridepublic boolean onDoubleTap(MotionEvent point) {drawLayer.removeAll();if(bIsMeasureLength == true){Polyline polyline = new Polyline();Point startPoint = null;Point endPoint = null;// 繪製完整的線段for(int i=1;i<points.size();i++){startPoint = points.get(i-1);endPoint = points.get(i);Line line = new Line();line.setStart(startPoint);line.setEnd(endPoint);polyline.addSegment(line, false);}Graphic g = new Graphic(polyline, lineSymbol);drawLayer.addGraphic(g);// 計算總長度String length = Double.toString(Math.round(polyline.calculateLength2D())) + " 米";Toast.makeText(map.getContext(), length, Toast.LENGTH_SHORT).show();}else{Polygon polygon = new Polygon();Point startPoint = null;Point endPoint = null;// 繪製完整的多邊形for(int i=1;i<points.size();i++){startPoint = points.get(i-1);endPoint = points.get(i);Line line = new Line();line.setStart(startPoint);line.setEnd(endPoint);polygon.addSegment(line, false);}Graphic g = new Graphic(polygon, fillSymbol);drawLayer.addGraphic(g);// 計算總面積String sArea = getAreaString(polygon.calculateArea2D());Toast.makeText(map.getContext(), sArea, Toast.LENGTH_SHORT).show();}// 其他清理工作btnClear.setEnabled(true);ptStart = null;ptPrevious = null;points.clear();tempPolygon = null;return false;}private String getAreaString(double dValue){long area = Math.abs(Math.round(dValue));String sArea = "";// 順時針繪製多邊形,面積為正,逆時針繪製,則面積為負if(area >= 1000000){double dArea = area / 1000000.0;sArea = Double.toString(dArea) + " 平方公裡";}elsesArea = Double.toString(area) + " 平方米";return sArea;}    }

如需要完整代碼,請在評論中留下郵箱。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.