androidgoogle-mapstouch-eventitemizedoverlay

in ItemizedOverlay how to handle onTouchEvent and onTap overlapping?


I am trying to place a marker on a map overlay and then present a pop up when the user selects the drawable. The problem is the events seem to overlap.when i tap(ontouch MotionEvent.ACTION_UP call) on marker then some time other marker gone and i also want to do like when popup is visible then no one marker can drag by user. Does anyone have any suggestions on how to make these events mutually exclusive?

Here is the code for the map activity:

    @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {
            final int action = event.getAction();
            final int x = (int) event.getX();
            final int y = (int) event.getY();
            boolean result = false;

            if (action == MotionEvent.ACTION_DOWN) {
                for (OverlayItem item : items) {
                    Point p = new Point(0, 0);

                    map.getProjection().toPixels(item.getPoint(), p);

                    if (hitTest(item, marker, x - p.x, y - p.y)) {
                        result = true;
                        inDrag = item;
                        items.remove(inDrag);
                        populate();

                        xDragTouchOffset = 0;
                        yDragTouchOffset = 0;

                        setDragImagePosition(p.x, p.y);
                        dragImage.setVisibility(View.VISIBLE);

                        xDragTouchOffset = x - p.x;
                        yDragTouchOffset = y - p.y;
                        Log.e("touch"," out ACTION_DOWN");
//                      onTap(index);
                        break;
                    }
                }
            } else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
//                      
                setDragImagePosition(x, y);      
                Log.e("touch"," out btn");
                result = true;
                flag = false;
            } else if (action == MotionEvent.ACTION_UP && inDrag != null) {
                 if (mLastMotionEventAction == MotionEvent.ACTION_DOWN){

                     // SOME TAP ACTIONS ...
                     dragImage.setVisibility(View.GONE);

                        GeoPoint pt = map.getProjection().fromPixels(
                                x - xDragTouchOffset, y - yDragTouchOffset);
                        OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
                                inDrag.getSnippet());

                        Log.e("touch"," out last" + pt.getLatitudeE6());
                        items.add(toDrop);
//                      onTap(pt, map);
//                      onTap();
                        populate();
                        inDrag = null;
                        result = true;
                        flag = false;
//                      if (result) {
//                          
//                      }
                    Log.e("pop up", "pop up");  
                    return false;
                } else {
                    return true;
                }

//              onTap(index);
            }
             mLastMotionEventAction = event.getAction();
            return (result || super.onTouchEvent(event, mapView));
        }


        private void setDragImagePosition(int x, int y) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
                    .getLayoutParams();

            lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
                    - yDragImageOffset - yDragTouchOffset, 0, 0);
            dragImage.setLayoutParams(lp);
        }
    }

And i write onTap but this method is not working.no error getting. i want to handle both method.


Solution

  • Finally i handled onTouchEvent and onTap overlapping with drag and pop-up with marker.

    @Override
                protected boolean onTap(final int index) {
                      if ( isPinch ){
                            return false;
                      }else{
        //          getMapView().setOnTouchListener(new OnTouchListener() {
        //              @Override
        //              public boolean onTouch(View arg0, MotionEvent arg1) {
        //                  if (!items.isEmpty()) {
        //                      if (view != null) {
        //                          view.setVisibility(View.GONE);
        //                          getMapView().invalidate();
        //                      }
        //                  }
        //                  getMapView().invalidate();
        //                  return true;
        //              }
        //          });
                    if (view != null) {
                        view.setVisibility(View.GONE);
                        getMapView().removeView(view);
                        getMapView().invalidate();
                        flag = false;
                        view = null;
                    }
                    view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
                    LinearLayout layout = (LinearLayout) view
                            .findViewById(R.id.balloon_main_layout);
                    layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
                    ImageView image = (ImageView) view
                            .findViewById(R.id.balloon_disclosure);
                    TextView text = (TextView) view
                            .findViewById(R.id.balloon_item_title);
                    text.setText(items.get(index).getTitle());
    
                    if (items.get(index).getTitle() != null
                            && items.get(index).getTitle().equals("Me") == false) {
                        image.setImageResource(R.drawable.mekr);
                    }
    
                    Projection projection = getMapView().getProjection();
                    Point point = new Point();
                    projection.toPixels(items.get(index).getPoint(), point);
    
                    int x = (int) (view.getWidth() / 2f);
                    int y = -bitMap.getHeight() - 3;
    
                    MapView.LayoutParams lp = new MapView.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
                                    .getPoint(), x, y,
                            MapView.LayoutParams.BOTTOM_CENTER);
                            getMapView().removeView(view);
                            getMapView().invalidate();
                            getMapView().addView(view, lp);
                            getMapView().invalidate();
    
                        view.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (!items.isEmpty()) {
                                    if (view != null) {
                                        view.setVisibility(View.GONE);
                                        getMapView().invalidate();
                                    }
                                }
                                getMapView().invalidate();
                            }
                        });
                            selectedIndex = index;
                            return true;           
                  }
                }
    
                @Override
                public boolean onTouchEvent(MotionEvent event, MapView mapView) {
                    final int action = event.getAction();
                    final int x = (int) event.getX();
                    final int y = (int) event.getY();
                    boolean result = false;
    
                    if (action == MotionEvent.ACTION_DOWN) {
                        for (OverlayItem item : items) {
                            Point p = new Point(0, 0);
    
                            map.getProjection().toPixels(item.getPoint(), p);
    
                            if (hitTest(item, marker, x - p.x, y - p.y)) {
                                result = true;
                                inDrag = item;
                                items.remove(inDrag);
                                populate();
    
                                xDragTouchOffset = 0;
                                yDragTouchOffset = 0;
    
                                setDragImagePosition(p.x, p.y);
                                dragImage.setVisibility(View.VISIBLE);
    
                                xDragTouchOffset = x - p.x;
                                yDragTouchOffset = y - p.y;
                                isPinch=false;
                                break;
                            }
                        }
                    } else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
    
                        if (view != null) {
                            if (view.getVisibility() != 0) {
                                Log.e("touch", " out move");
                                setDragImagePosition(x, y);
                                result = true;
                                isPinch=true;
                            }else{
                                setDragImagePosition(x, y);
                                isPinch=false;
                            }
                        }else{
                            setDragImagePosition(x, y);
                            result = true;
                            isPinch=true;
                        }
                    } else if (action == MotionEvent.ACTION_UP && inDrag != null) {
                        dragImage.setVisibility(View.GONE);
    
                        GeoPoint pt = map.getProjection().fromPixels(
                                x - xDragTouchOffset, y - yDragTouchOffset);
                        OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
                                inDrag.getSnippet());
    
                        Log.e("touch", " out last" + pt.getLatitudeE6());
                        items.add(toDrop);
                        populate();
                        inDrag = null;
                        result = true;
                        isPinch = false;
                    }
                        return (isPinch || super.onTouchEvent(event,mapView));
                }
    
                private void setDragImagePosition(int x, int y) {
                    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
                            .getLayoutParams();
    
                    lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
                            - yDragImageOffset - yDragTouchOffset, 0, 0);
                    dragImage.setLayoutParams(lp);
                }
            }