Question

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.

Was it helpful?

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);
            }
        }

OTHER TIPS

Try out below code :

 /**
   * Class used to plot the start and end points on the map
   * 
  */
 public class MapendPointsOverlay extends ItemizedOverlay<OverlayItem> {
    private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
    private BalloonOverlayView m_balloonView;
    private OverlayItem m_overlayitem;
    private Context m_context;
    private int m_viewOffset;
    private MapController m_mapController;
    private MapView m_mapView;
    private int m_screenWidth;
    private int m_screenHeight;
    private Display m_screen;
    private boolean m_fromroutepath = false;
public MapendPointsOverlay(Drawable p_drawable) {
    super(p_drawable);
}
public MapendPointsOverlay(Drawable p_defaultMarker, Context p_context,
        MapView p_mapview, boolean p_fromroutepath) {
    super(boundCenterBottom(p_defaultMarker));
    m_context = p_context;
    m_mapView = p_mapview;
    m_mapController = p_mapview.getController();
    m_viewOffset = 35;
    m_fromroutepath = p_fromroutepath;
    m_screen = ((WindowManager) m_context               .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    m_screenWidth = m_screen.getWidth();
    m_screenHeight = m_screen.getHeight();
}
/**
 * Method that creates an OverlayItem from the list of OverlayItem at index
 * given by <b>p_arg0</b>
 */
@Override
protected OverlayItem createItem(int p_arg0) {
    return m_overlays.get(p_arg0);
}
@Override
public int size() {
    return m_overlays.size();
}
@Override
protected boolean onTap(int p_index) {
    boolean m_isRecycled;
    GeoPoint m_point;
    m_point = createItem(p_index).getPoint();
    m_overlayitem = m_overlays.get(p_index);
    if (m_balloonView == null) {
        m_balloonView = new BalloonOverlayView(m_context, m_viewOffset);
        m_isRecycled = false;
    } else {
        m_isRecycled = true;
    }
    m_balloonView.setVisibility(View.GONE);
    m_balloonView.setData(m_overlayitem);
    m_balloonView.setVisibility(View.VISIBLE);
    MapView.LayoutParams m_params = new MapView.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, m_point,MapView.LayoutParams.BOTTOM_CENTER);
    m_params.mode = MapView.LayoutParams.MODE_MAP;
    List<Overlay> m_mapOverlays = m_mapView.getOverlays();
    if (m_mapOverlays.size() > 1) {
        hideOtherBalloons(m_mapOverlays);
    }
    if (m_isRecycled) {
        m_balloonView.setLayoutParams(m_params);
    } else {
        m_mapView.addView(m_balloonView, m_params);
    }
    m_mapController.animateTo(m_point);
    m_mapController.setCenter(m_point);
    return true;
}
/**
 * Sets the visibility of this overlay's balloon view to GONE.
 */
private void hideBalloon() {
    if (m_balloonView != null) {
        m_balloonView.setVisibility(View.GONE);
    }
}
/**
 * Hides the balloon view for any other BalloonItemizedOverlay instances
 * that might be present on the MapView.
 * 
 * @param p_overlays
 *            - list of overlays (including this) on the MapView.
 */
private void hideOtherBalloons(List<Overlay> p_overlays) {
    for (Overlay m_overlay : p_overlays) {
        if (m_overlay instanceof MapendPointsOverlay && m_overlay != this) {
            ((MapendPointsOverlay) m_overlay).hideBalloon();
        }
    }
}
/**
 * Override this method to handle a "tap" on a balloon. By default, does
 * nothing and returns false.
 * 
 * @param p_index
 *            - The index of the item whose balloon is tapped.
 * @return true if you handled the tap, otherwise false.
 */
protected boolean onBalloonTap(int p_index) {
    return false;
}
   }

BallonOverlay class that open the information window.

public class BalloonOverlayView extends FrameLayout
  { 
private LinearLayout m_layout;
private TextView m_title;
private TextView m_snippet; 
/**
 * Create a new BalloonOverlayView.
 * 
 * @param p_context
 *            - The activity context.
 * @param p_balloonBottomOffset
 *            - The bottom padding (in pixels) to be applied when rendering this view.
 */
public BalloonOverlayView(Context p_context, int p_balloonBottomOffset)
{
    super(p_context);
    // m_ctx = context;     
    setPadding(10, 0, 10, p_balloonBottomOffset);       
    m_layout = new LinearLayout(p_context);
    m_layout.setVisibility(VISIBLE);        
    LayoutInflater m_inflater = (LayoutInflater) p_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View m_view = m_inflater.inflate(R.layout.balloonoverlay, m_layout);        
    m_title = (TextView) m_view.findViewById(R.id.balloon_item_title);      
    m_snippet = (TextView) m_view.findViewById(R.id.balloon_item_snippet);
    m_snippet.setVisibility(View.GONE);     
    ImageView m_close = (ImageView) m_view.findViewById(R.id.close_img_button);
    m_close.setOnClickListener(new OnClickListener(){
        public void onClick(View v)
        {
            m_layout.setVisibility(GONE);
        }
    });     
    FrameLayout.LayoutParams m_params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    m_params.gravity = Gravity.NO_GRAVITY;
    addView(m_layout, m_params);        
}
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top