1

I'm trying to get a hold of touch events that happen in a MapView, however, the listener is never called.

MapActivity

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

    private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";

    private MapView mapView;
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        // *** IMPORTANT ***
        // MapView requires that the Bundle you pass contain _ONLY_ MapView SDK
        // objects or sub-Bundles.
        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
        }
        mapView = (MapView) findViewById(R.id.view_map);
        mapView.onCreate(mapViewBundle);

        mapView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                Log.d("debug", "Listener");
                int action = MotionEventCompat.getActionMasked(event);

                switch (action) {

                    case (MotionEvent.ACTION_DOWN) :
                        Log.d("debug","Action was DOWN");
                        break;
                    case (MotionEvent.ACTION_MOVE) :
                        Log.d("debug","Action was MOVE");
                        break;
                    case (MotionEvent.ACTION_UP) :
                        Log.d("debug","Action was UP");
                        break;
                    default:
                        break;
                }
                return true;
            }
        });

        mapView.getMapAsync(this);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
        if (mapViewBundle == null) {
            mapViewBundle = new Bundle();
            outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
        }

        mapView.onSaveInstanceState(mapViewBundle);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {

        map = googleMap;
    }
}

Why is the listener never called? What am I doing wrong?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Tirafesi
  • 1,087
  • 14
  • 24
  • Mapviews don't seem to work with Touch Listeners. You can use Click Listener or you can [create a way to intercept touch events](http://stackoverflow.com/questions/14013002/google-maps-android-api-v2-detect-touch-on-map) – Sammy T May 12 '17 at 18:07

1 Answers1

0

Register an Overlay to the MapView and add TouchEvent to That

 public void onCreate(Bundle savedInstanceStates){       
    super.onCreate(savedInstanceStates);
    setContentView(R.layout.map);

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

    mapOverlay myOverlay = new mapOverlay();
    List<Overlay> overlays = mMapView.getOverlays();        
    overlays.add(myOverlay);
}        



class mapOverlay extends com.google.android.maps.Overlay{
    @Override

    public boolean onTouchEvent(MotionEvent event, MapView mapview){

        switch (event.getAction()) {

                    case (MotionEvent.ACTION_DOWN) :
                        Log.d("debug","Action was DOWN");
                        break;
                    case (MotionEvent.ACTION_MOVE) :
                        Log.d("debug","Action was MOVE");
                        break;
                    case (MotionEvent.ACTION_UP) :
                        Log.d("debug","Action was UP");
                        break;
                    default:
                        break;
                }
                return true;
    }
}
rafsanahmad007
  • 23,062
  • 6
  • 43
  • 58
  • ``Log.d("debug", "Listener")`` doesn't even get called. The problem is that the listener isn't being called – Tirafesi May 12 '17 at 18:17