animation123 animation123 - 4 months ago 30
Android Question

Why event listener not working on Google map marker in Android?

Currently i am working on Google map but when i am giving click listener to the marker the event is not triggering.

@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {

View view = getActivity().getLayoutInflater().inflate(R.layout.map_marker_layout, null);

mNameLayout = (RelativeLayout) view.findViewById(R.id.map_name_layout);

mNameLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
}
});

return view;
}

@Override
public View getInfoContents(Marker marker) {
return null;
}
});
}


Please kindly go through my code and suggest me some solution.

Answer

onMapReady Set onClick listner like this.

  googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
       Toast.makeText(getActivity(), "Infowindow clicked", Toast.LENGTH_SHORT).show();
        }
    });
    googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
           Toast.makeText(getActivity(), "Marker Clicked", Toast.LENGTH_SHORT).show();
            return false;
        }
    });