tags:

views:

27

answers:

1

I'm working on an Android class that extends MapActivity. I have set the OnTouchListener and everything works okay for the first MotionEvent. After the first MotionEvent, the system stops either generating MotionEvents or receiving them or both. Does anybody know what I have to do to keep receiving MotionEvents? Thanks.

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mapView.setOnTouchListener(new View.OnTouchListener( ) {    
        @Override public boolean onTouch(View v, MotionEvent event) {
            Log.i(TAG, "onTouch called");

            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "ACTION_DOWN: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "ACTION_UP: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.i(TAG, "ACTION_CANCEL: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_OUTSIDE:
                Log.i(TAG, "ACTION_OUTSIDE: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "ACTION_MOVE: x = " + event.getX() + ", y = " + event.getY());
                break;
            }

            v.onTouchEvent(event);
            return true;   // I also tried return false here, that didn't fix anything
        }
    });
A: 

You should not put the event handling in the onCreate method since the onCreate method is executet on start up. To enable touching or tabbing for your map you should use Map Overlay and here, Using Google Maps in Android.

Write you MapOverlay class within you MapActivity:

class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
           //...
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---

            return false;
        }        
    }

...and then just add the overlay to the MapActivity, in your onCreate method:

MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);        
mapView.invalidate();
ArtWorkAD