ArcGIS for Android 中实现要素绘制时固定MapView

  最近在项目中遇到这么一个情况,在MapView中要求实现绘制点、线、面。

  在这里面就会遇到这么一个问题,绘制折线和多边形型时,每点击一个点屏幕就会跟着晃,使用起来很不方便(使用Note2 触控笔),所以有没有一种方法让我点击绘制界面开始,地图界面锁死,在绘制结束时ontouch事件才响应。

  现在先说说思路,一般来说我设置MapView的OnTouch事件为Null,屏幕就不会跟着动了,但是同时OnTouch事件也没法使用了,这个方法不可用。但是流状线和流状面绘制是怎么实现的呢?经过查询,在OnTouch的中有以下两个方法:

  public boolean onDragPointerMove(MotionEvent from, MotionEvent to) 

  public boolean onDragPointerUp(MotionEvent from, MotionEvent to) 

这两个方法分别对应了按下时的滑动事件和滑动松开事件,我们让这两个方法在绘制折线和自定义面时返回false,屏幕就不会跟着跑了 。

MapOnTouchListener类如下:

  public class MyTouchListener extends MapOnTouchListener 
    {
        MultiPath polyline,polygon,line,freepolygon;
        DrawType type = DrawType.None;
        Point startPoint = null;
        MapView map = null;
        DrawWidget drawwidget =null;
        
        private View calloutView =null;
      
        int graphicFreelineId = 0;
        int graphicLineId = 0;
        int graphicFreePloygonId = 0;
        int graphicPloygonId = 0;
   
        public MyTouchListener(Context context, MapView view,DrawWidget  d,View v) {  
            super(context, view);  
            map = view;
            drawwidget=d;
            calloutView = v;
       }
     
        public void setType(DrawType type) {
          this.type = type;
        }
        public DrawType getType() {
          return this.type;
        }
        
        @Override
        public boolean onSingleTap(MotionEvent e) 
        {
            if(type == DrawType.Point) 
            {
                Geometry geo = map.toMapPoint(new Point(e.getX(), e.getY()));
                Graphic gra = addGeometryToLocalDB(geo);            
                if(gra!=null){
                    GraUID =mGraphicsLayer.addGraphic(gra);//添加要素至数据库
                    //弹出callout窗口
                    Point coordinate=(Point) geo;
                    drawwidget.showCallout(coordinate, calloutView);        
                    mMyTouchListener.setType(DrawType.None);
                }         
                return true;
            }else if(type == DrawType.Line){
                //获取屏幕点击坐标点
                Point point = map.toMapPoint(new Point(e.getX(), e.getY()));              
                if (startPoint == null) {                    
                    startPoint = point;        
                    line = new Polyline();
                    line.startPath(point);        
                    //添加节点信息
                    Graphic graphic = new Graphic(point,new SimpleMarkerSymbol(Color.BLACK,5,STYLE.CIRCLE));
                    tmpLayer.addGraphic(graphic);
                    //添加线要素             
                    Graphic graphic_line = new Graphic(line,FeatureSymbol.lineSymbol_new);
                    graphicLineId = mGraphicsLayer.addGraphic(graphic_line);    
                } else{                    
                    //添加线要素节点
                    Graphic graphic_t = new Graphic(point,new SimpleMarkerSymbol(Color.BLACK,5,STYLE.CIRCLE));
                    tmpLayer.addGraphic(graphic_t);    
                    //更新线信息
                    line.lineTo(point);
                    mGraphicsLayer.updateGraphic(graphicLineId, line); 
                }               
            }else if(type == DrawType.Polygon){
                //获取屏幕点击坐标点
                Point point = map.toMapPoint(new Point(e.getX(), e.getY()));              
                if (startPoint == null) {
                    startPoint = point;        
                    polygon = new Polygon();
                    polygon.startPath(point);        
                    //添加节点信息
                    Graphic graphic = new Graphic(point,new SimpleMarkerSymbol(Color.BLACK,5,STYLE.CIRCLE));
                    tmpLayer.addGraphic(graphic);
                    //添加多边形要素             
                    Graphic graphic_polygon = new Graphic(polygon,FeatureSymbol.polygonSymbol_new);
                    graphicPloygonId = mGraphicsLayer.addGraphic(graphic_polygon);          
                } else{                    
                    //添加要素节点
                    Graphic graphic_t = new Graphic(point,new SimpleMarkerSymbol(Color.BLACK,5,STYLE.CIRCLE));
                    tmpLayer.addGraphic(graphic_t);    
                    //更新多边形信息
                    polygon.lineTo(point);
                    mGraphicsLayer.updateGraphic(graphicPloygonId, polygon); 
                }               
            }
            return false;
        }
        
        @Override
        public boolean onDoubleTap(MotionEvent event) {
            tmpLayer.removeAll();
            if (type == DrawType.Line) {    
                if(line!=null){
                    Graphic gral = addGeometryToLocalDB(line);//添加要素至数据库
                    if(gral!=null){
                        mGraphicsLayer.updateGraphic(graphicLineId,gral); 
                        drawwidget.showCallout(startPoint, calloutView);
                    }else{
                        mGraphicsLayer.removeGraphic(graphicLineId);
                    }    
                }else{
                    Toast.makeText(DrawWidget.super.context,"未添加任务要素!",Toast.LENGTH_SHORT).show();
                }
                GraUID =graphicLineId;
                startPoint = null;
                line = null;
                mMyTouchListener.setType(DrawType.None);
                 DrawWidget.super.showMessageBox("折线绘制结束!");
                return true;
            }else if(type == DrawType.Polygon){        
                if(polygon!=null){
                    Graphic gra = addGeometryToLocalDB(polygon);
                    if(gra!=null){
                        mGraphicsLayer.updateGraphic(graphicPloygonId,gra ); //添加要素至数据库    
                        //弹出callout窗口
                        drawwidget.showCallout(startPoint, calloutView);  
                    }else{
                        mGraphicsLayer.removeGraphic(graphicPloygonId);
                    }  
                }else{
                    Toast.makeText(DrawWidget.super.context,"未添加任务要素!",Toast.LENGTH_SHORT).show();
                }
                GraUID =graphicPloygonId;
                startPoint = null;
                polygon = null;
                mMyTouchListener.setType(DrawType.None);
                DrawWidget.super.showMessageBox("多边形绘制结束!");
                return true;        
            }
            return super.onDoubleTap(event);
        }
        
        //@Override
        public boolean onDragPointerMove(MotionEvent from, MotionEvent to) 
        {
            Point mapPt = map.toMapPoint(to.getX(), to.getY());
            if (type == DrawType.Freeline) 
            {
                if (startPoint == null) 
                {
                    polyline = new Polyline();
                    startPoint = map.toMapPoint(from.getX(), from.getY());
                    polyline.startPath((float) startPoint.getX(), (float) startPoint.getY());
                    graphicFreelineId = mGraphicsLayer.addGraphic(new Graphic(polyline,FeatureSymbol.lineSymbol_new));
                }
                polyline.lineTo((float) mapPt.getX(), (float) mapPt.getY());
                mGraphicsLayer.updateGraphic(graphicFreelineId,new Graphic(polyline,FeatureSymbol.lineSymbol_new));
                return true;
            }
            else if (type == DrawType.FreePolygon) 
            {
                //polygonSymbol.setAlpha(80);
                if (startPoint == null) 
                {
                    freepolygon = new Polygon();
                    startPoint = map.toMapPoint(from.getX(), from.getY());
                    freepolygon.startPath((float) startPoint.getX(), (float) startPoint.getY());
                    graphicFreePloygonId = mGraphicsLayer.addGraphic(new Graphic(freepolygon,FeatureSymbol.polygonSymbol_new));
                }
                freepolygon.lineTo((float) mapPt.getX(), (float) mapPt.getY());
                mGraphicsLayer.updateGraphic(graphicFreePloygonId, new Graphic(freepolygon,FeatureSymbol.polygonSymbol_new));
                return true;
            }else if(type == DrawType.Polygon||type == DrawType.Line){
                return false;//返回false,使屏幕不滑动固定死(两个位置)
            }
            return super.onDragPointerMove(from, to);
        }

        @Override
        public boolean onDragPointerUp(MotionEvent from, MotionEvent to) 
        {
            if(type == DrawType.Line ||type == DrawType.Polygon){
                return false;//返回false,使屏幕不滑动固定死(两个位置)
            }else if(type ==DrawType.Freeline ){
                Graphic gral = addGeometryToLocalDB(polyline);//添加要素至数据库
                if(gral!=null){
                    mGraphicsLayer.updateGraphic(graphicFreelineId,gral);
                    drawwidget.showCallout(startPoint, calloutView);
                }else{
                    mGraphicsLayer.removeGraphic(graphicFreelineId);
                }
                GraUID = graphicFreelineId;
                startPoint = null;
                polyline = null;//流状线
                DrawWidget.super.mapView.setOnTouchListener(mDefaultMyTouchListener);
            }else if(type == DrawType.FreePolygon){
                Graphic gra = addGeometryToLocalDB(freepolygon);
                if(gra!=null){
                    mGraphicsLayer.updateGraphic(graphicFreePloygonId,gra);//添加要素至数据库
                    drawwidget.showCallout(startPoint, calloutView);
                }else{
                    mGraphicsLayer.removeGraphic(graphicFreePloygonId);
                }            
                GraUID = graphicFreePloygonId;
                startPoint = null;
                freepolygon = null;//流状面
                DrawWidget.super.mapView.setOnTouchListener(mDefaultMyTouchListener);
            } 
            return super.onDragPointerUp(from, to);
        }

 

posted on 2013-12-30 15:03  gis-luq  阅读(1287)  评论(0编辑  收藏  举报