Hick
Hick

Reputation: 36404

OnTouch and OnTap overlapping. WHat to do?

This is my code for OnTouch. I've another onTap method to display pop ups. But, it is not getting fired as OnTouch is overlapping on OnTap. Thus, how do I convert this OnTouchEvent function to OnTap. Because, if I remove this onTouch function, I will not be able to drag the marker.

 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;
            p =  map.getProjection().fromPixels(x, y);

            newP = p;
                  if (action==MotionEvent.ACTION_DOWN) {

                    for (OverlayItem item : items) {
                      Point pa=new Point(0,0);

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

                          //I maintain the hitTest's bounds so you can still
                          //press near the marker
                      if (hitTest(item, marker, x-pa.x, y-pa.y)) {
                        result=true;

                        inDrag=item;

                        items.remove(inDrag);
                        populate();

                            //Instead of using the DragImageOffSet and DragTouchOffSet
                            //I use the x and y coordenates from the Point
                        setDragImagePosition1(x, y);

                        dragImage.setVisibility(View.VISIBLE);

                        break;
                      }
                    }
                  }
                  else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {
                    setDragImagePosition1(x, y);

                    result=true;
                  }
                  else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
                    dragImage.setVisibility(View.GONE);

                        //I get the geopoint without using any OffSet, directly with the 
                        //Point coordenates
                    p=map.getProjection().fromPixels(x,y);

                    OverlayItem toDrop=new OverlayItem(p, inDrag.getTitle(),
                                                       inDrag.getSnippet());
                    Drawable orig = inDrag.getMarker(0);

                        //In my case I had down heading Arrows as markers, so I wanted my 
                        //bounds to be at the center bottom
                    if( orig != null)
                        toDrop.setMarker(boundCenterBottom(orig));

                    items.add(toDrop);
                    populate();

                    inDrag=null;
                    result=true;
                  }


               return(result || super.onTouchEvent(event, mapView));



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

              //Instead of using OffSet I use the Point coordenates.
              //I want my arrow to appear pointing to the point I am moving, so 
              //I set the margins as the Point coordenates minus the Height and half the
              //width of my arrow.
          lp.setMargins(x-(dragImage.getWidth()/2),y-dragImage.getHeight(), 0, 0);


          dragImage.setLayoutParams(lp);
        }

Upvotes: 0

Views: 420

Answers (1)

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23972

After onTouch was fired X times, calculate the average distance that pointer was moved. If it less than some Y value, than start onTap event, else start dragging.

Upvotes: 1

Related Questions