Henry
Henry

Reputation: 1042

Touch listener on Google Map Markers Android

How can I add a Tab/Touch/Click listener on the Markers I have on my Google Map in android program. For example in the below image I have one marker on google maps and when clicked, I want to bring up a toast saying this has been clicked

https://i.sstatic.net/Bc7Lp.png

I have tried onMapClickListener but that does not work.

map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {

    }
  }

Upvotes: 2

Views: 3936

Answers (2)

user3515851
user3515851

Reputation: 469

All markers in Google Android Maps Api v2 are clickable. You don't need to set any additional properties to your marker. What you need to do - is to register marker click callback to your googleMap and handle click within callback:

public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener{
private Marker myMarker;    

private void setUpMap()
{
    .......
    googleMap.setOnMarkerClickListener(this);

    myMarker = googleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title("My Spot")
                .snippet("This is my spot!")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    ......
}

@Override
public boolean onMarkerClick(final Marker marker) {

    if (marker.equals(myMarker)) 
    {
        //handle click here
    }
}

}

Upvotes: 4

user3515851
user3515851

Reputation: 469

Use this

map.setOnMarkerClickListener(new OnMarkerClickListener()
            {

                @Override
                public boolean onMarkerClick(Marker arg0)            
                         Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
                    return true;
                }

            });    

This will help to you :)

Upvotes: 4

Related Questions