1

I'm new to Android and I want to handle touches on mapView like this post Google Maps Android API v2 - detect touch on map

But it quite difference, in my cases, I use fragment class to handle all my map business.

My layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ImageButton
        android:id="@+id/buttonTracking"
        ... />
</LinearLayout>

I have already try to use setOnTouchListener but it not work

public class MapsFragment extends Fragment implements PermissionsFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        ll = (LinearLayout) inflater.inflate(R.layout.fragment_maps, container, false);
        mMapView = (MapView) ll.findViewById(R.id.map);
        ll.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                Log.e("INFO", "Touched");
                return true;
            }
        });
    }
}

Did I miss anythings? Please help! Thanks

Community
  • 1
  • 1
Jonny Vu
  • 1,220
  • 1
  • 12
  • 29

1 Answers1

1

Set click listener on GoogleMap object not on MapView

GoogleMap map;

map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(final LatLng clickCoords) {
          //do your code here
        }
};
Atul Vasudev A
  • 453
  • 3
  • 18
  • 1
    clickListener is not enough for detect user actions. I want to detect the user drag map. To solve this problem, I am using `setOnCameraMoveStartedListener` with the reason = `REASON_GESTURE`. – Jonny Vu Jan 17 '17 at 14:13