美文网首页
Arcgis For Android 标记物触摸事件示例

Arcgis For Android 标记物触摸事件示例

作者: fringe阿萝 | 来源:发表于2020-07-23 15:17 被阅读0次
       //先保存原来的触摸事件
        View.OnTouchListener listenerOri=  mMapView.getOnTouchListener();
        mMapView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
               //  以下展示了如何为标记物Graphic 添加拖动事件,懂的都懂
                boolean isDrag = false;
                float touchThresh =1000;
                float fromx;
                float fromy;
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        isDrag = true;
                        float x = motionEvent.getX();
                        float y = motionEvent.getY();
                        fromx=x;
                        fromy=y;
                        Log.i(getClass().getName(), "x,y " + x + " " + y);
                        //  mMapView.locationToScreen()
                      //从要添加事件的层里去遍历
                        for (Graphic gr : mNavigationGraphicsOverlay.getGraphics()) {
                            android.graphics.Point po_s = mMapView.locationToScreen((Point) gr.getGeometry());
                            Log.i(getClass().getName(), "po_s " + po_s.x + " " + po_s.y);
                            if(gr.getAttributes().get("id")!=null){
                                Log.i(getClass().getName(), "id " +gr.getAttributes().get("id"));
                            }
                            float dis = (float) (Math.pow(po_s.x-x,2) +    Math.pow(po_s.y-y,2));                  
                        //如果Graphic 与touch点距离足够近,就判断为点到了,并给  Graphic 添加标记
                          if(dis<touchThresh){
                                gr.getAttributes().put("touchin", true);
                                Log.i(getClass().getName(), "touchin " +gr.getAttributes().get("id"));
                            }
                            else{
                                Log.i(getClass().getName(), "dis " +dis);
                            }
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        isDrag = false;
                  
                        for (Graphic gr : mNavigationGraphicsOverlay.getGraphics()) {
                            boolean touchin = gr.getAttributes().get("touchin")!=null &&  gr.getAttributes().get("touchin").toString().equals("true");
                            if (touchin){
                                Point po = (Point) gr.getGeometry();
                               Point po_wgs= (Point) GeometryEngine.project(po,SpatialReference.create(4326));
                               double[] arr_po_wgs = WGS2GCJ.gps84_To_Gcj02(po_wgs.getX(),po_wgs.getY());
                               if(gr.getAttributes().get("id")!=null && gr.getAttributes().get("id").toString().equals("1")){
                                   navFrom_gcj = arr_po_wgs;
                               }
                               else if(gr.getAttributes().get("id").toString().equals("2")){

                               }
                            }
                            gr.getAttributes().put("touchin", false);
                        }

                        break;
            //触摸中,更新Graphic 位置
                    case MotionEvent.ACTION_MOVE:
                        for (Graphic gr : mNavigationGraphicsOverlay.getGraphics()) {
                             boolean touchin = gr.getAttributes().get("touchin")!=null &&  gr.getAttributes().get("touchin").toString().equals("true");
                             float cx= motionEvent.getX();
                             float cy = motionEvent.getY();
                             if (touchin){
                                 Point po = mMapView.screenToLocation(new android.graphics.Point((int)cx,(int)cy));
                                 gr.setGeometry(po);
                                 Log.i(getClass().getName(), "touchin "+po.toString());
                            //拖动Graphic 时,mapview本身无须动,所以返回true,阻止触摸事件传递
                                 return true;
                             }
                             else {
                                 Log.i(getClass().getName(), "touchin false" );
                             }
                        }
                        break;
                }
          //最后返回之前保存的原版事件,以防mapview本身的触摸事件失效;
                return listenerOri.onTouch(mMapView,motionEvent);
            }
        });

相关文章

网友评论

      本文标题:Arcgis For Android 标记物触摸事件示例

      本文链接:https://www.haomeiwen.com/subject/ozlhlktx.html