S Haque
S Haque

Reputation: 7281

how to bind a touch event with mapfragment in android

I want to detect two scenario 1. when a user tap into a google map 2. when a user release that tap from the map It's like the touchevent which we use for other view like TextView, Linearlayout etc... -Thank you enter image description here

Upvotes: 1

Views: 837

Answers (2)

skywall
skywall

Reputation: 4005

You can create some kind of overlay over MapFragment and set onTouchListener to it.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <View
        android:id="@+id/mapOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

Java code:

View mapOverlay = (View) findViewById(R.id.mapOverlay);        
mapOverlay.setOnTouchListener(new View.OnTouchListener() {
    @Override public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // handle tap
                break;
            case MotionEvent.ACTION_UP:
                // handlerelease
                break;
        }

        return false; // return false if you want propagate click to map
    }
});

Upvotes: 1

fluffyBatman
fluffyBatman

Reputation: 6704

For the first scenario, use this. To get it working is pretty much self explanatory but here is an example for your convenience.

gMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {
            // TODO Auto-generated method stub
            Log.d("LatLng", arg0.latitude + "-" + arg0.longitude);
        }
    });

Upvotes: 0

Related Questions