Gaucho Gaucho - 1 year ago 129
Android Question

Google Maps Android API v2 - detect touch on map

I can't find an example on how to intercept the map touch on the new Google Maps API v2.

I need to know when the user touches the map in order to stop a thread (the centering of the map around my current location).
What would a starting point be?

Answer Source

@ape wrote an answer here on how to intercept the map clicks, but I need to intercept the touches, and then he suggested the following link in a comment of its answer, How to handle onTouch event for map in Google Map API v2?.

That solution seems to be a possible workaround, but the suggested code was incomplete. For this reason I rewrote and tested it, and now it works.

Here it is the working code:

I created the class

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MySupportMapFragment extends SupportMapFragment {
    public View mOriginalContentView;
    public TouchableWrapper mTouchView;

    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);
        mTouchView = new TouchableWrapper(getActivity());
        return mTouchView;

    public View getView() {
        return mOriginalContentView;

I even created the class

import android.content.Context;
import android.view.MotionEvent;
import android.widget.FrameLayout;

public class TouchableWrapper extends FrameLayout {

    public TouchableWrapper(Context context) {

    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                  MainActivity.mMapIsTouched = true;

            case MotionEvent.ACTION_UP:
                  MainActivity.mMapIsTouched = false;
        return super.dispatchTouchEvent(event);

In the layout I declare it this way:

<fragment xmlns:android=""

Just for test in the main Activity I wrote only the following:

public class MainActivity extends FragmentActivity {
    public static boolean mMapIsTouched = false;

    protected void onCreate(Bundle savedInstanceState) {