Why does MapView onTouchListener only start once?

Possible duplicate:
OnTouch in MapView only launches for the first time

I want to detect every touch made by the user on the map, and so I registered a listener for the MapView instance. However, this listener only receives once, after which 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; } }); 
+4
source share
2 answers

Have you tried to return true instead of false? Maby has a problem, since you are not actually consuming a touch event, that it lingers around what can no longer be called.

+1
source

I could never get it to work more than once. I ended up adding an overlay that does nothing and puts onTouchEvent () in the overlay. It 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; } } } 
+1
source

Source: https://habr.com/ru/post/1398121/


All Articles