Mark Caveneau
Mark Caveneau

Reputation: 39

Android - Set listener marker

in my googleMap there are 5 markers. Is there a method by which I can set a listener just on one of them? The code below shows how to do this, but only if there is a single marker:

GoogleMap mMap
Marker marker = mMap.addMarker(
   new MarkerOptions()
   .position(new LatLng(dLat, dLong))
   .title("Your title")                                       
   .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin)));

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker m) {
}
}

Upvotes: 0

Views: 81

Answers (1)

Sachin Rajput
Sachin Rajput

Reputation: 4344

When you are adding marker , you can add Tag to each Marker,

like this

 marker1 = mMap.addMarker(new MarkerOptions()
                    .position(yourPosition)
                    .title("yourTitle");
  marker1.setTag("YourTag");

and then you can identify which Marker is clicked by accessing the Tag value in OnMarkerClickListener.

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker m) {

                       if(m.getTag()=="YourTag"){
                        //Perfom your operation here
                       }else if(m.getTag()=="AnotherTag"){
                        //Perfom your operation here
                       }
    }

Upvotes: 1

Related Questions