4

Possible Duplicate:
OnTouch in MapView only fires the first time

I want to detect every touch made by the user to the map and hence I have registered a listener for an instance of MapView. However, this listener only get called once, after that I need to close the application if I want it to work again. I am using OsmDroid.

        mMapView = (MapView) findViewById(R.id.mapview); 

    mMapView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("OnTouch MapView Listener!");
            return false;
        }
    });
Community
  • 1
  • 1
fulupr
  • 129
  • 2
  • 11

2 Answers2

1

Have you tried returning true instead of false? Mabey there is an issue since your not actually consuming the touch event that it's lingering around which is prevent anymore from being called.

Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
  • yes I did tried that but it does not worked. – fulupr Feb 23 '12 at 21:00
  • Hum. Perhaps you can try onTouchEvent instead? You can just override this method in the MapActivity – Frank Sposaro Feb 23 '12 at 21:02
  • I cannot @Override the onTouchEvent method because the activity just extends Activity so it gives me an error that says "The method onTouchEvent(MotionEvent, MapView) of type MainActivity must override a superclass method". – fulupr Feb 23 '12 at 21:09
  • yeah. You must extend MapActivity instead. That will let you override that method – Frank Sposaro Feb 23 '12 at 21:15
  • I tried to extends MapActivity but I believe that due to the fact that I am using OsmDroid it is not letting me. Any idea? – fulupr Feb 23 '12 at 21:48
1

I could never get it to work more than once either. I ended up adding an overlay which does nothing and putting an onTouchEvent() in the overlay. This worked

public class OsmdroidDemoMap extends Activity {

    private MapView mMapView;
    private MapController mMapController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.osm_main);
        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.setTileSource(TileSourceFactory.MAPNIK);
        mMapView.setBuiltInZoomControls(true);
        mMapView.setMultiTouchControls(true);
        mMapController = mMapView.getController();
        mMapController.setZoom(13);
        GeoPoint gPt = new GeoPoint(51500000, -150000);
        mMapController.setCenter(gPt);
        MapOverlay movl = new MapOverlay(this);
        mMapView.getOverlays().add(movl);
    }

    public class MapOverlay extends org.osmdroid.views.overlay.Overlay {

        public MapOverlay(Context ctx) {super(ctx);}

        @Override
        protected void draw(Canvas c, MapView osmv, boolean shadow) { }

        @Override
        public boolean onTouchEvent(MotionEvent e, MapView mapView) {
            if(e.getAction() == MotionEvent.ACTION_DOWN)
                Toast.makeText(OsmdroidDemoMap.this, "Touched", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}
NickT
  • 23,844
  • 11
  • 78
  • 121
  • I tried doing that but `mMapView.getOverlays().add(movl);` make my map to tilt every time I touch it. I suspect that the reason is the fact that I also implement ItemizedOverlay from OsmDroid. – fulupr Feb 24 '12 at 17:48