RmK
RmK

Reputation: 1438

How to disable setOnMapClickListener in google maps v2?

I have an action bar icon (Plus icon) clicking on which I have enabled (called) setOnMapClickListener inside which I create markers on the map based onClicks on the map.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.getItemId() == R.id.action_addItem) {

        invalidateOptionsMenu();



            mMap.setOnMapClickListener(new OnMapClickListener() {

                @Override
                public void onMapClick(LatLng point) {

                if(canAddMarker){

                    mMap.animateCamera(CameraUpdateFactory.newLatLng(point));

                    mMap.addMarker(new MarkerOptions().position(point).title("Test Title").snippet("Test snippet"));


                }


                }
            });

    else {

               //Code to disable listener
    }

    }

The problem is once I click on any other icon on action bar, I'd like the listener to stop/disabled so that any more touch on the map shouldn't result in new markers being created.

Any thoughts on how to disable listener ?

Upvotes: 0

Views: 2201

Answers (1)

Pedro Oliveira
Pedro Oliveira

Reputation: 20500

To disable a listener simple set it to null since it will only be called if it's not null.

mMap.setOnMapClickListener(null);

Upvotes: 2

Related Questions