-1

tell me, how can I correctly catch a click on a certain marker on the Google Maps map and then set the action? Let's say there are three variables Marker One; Two; Three; I did so, but works only with the third-fourth click on the marker:

    public void onMapReady(GoogleMap googleMap) {
...
One = mMap.addMarker(new MarkerOptions()
        .position(lat));
One.setTag(0);
// Set a listener for marker click.
mMap.setOnMarkerClickListener(this);

}

public boolean onMarkerClick(final Marker marker) {

ExampleBottomSheetDialog bottomSheet = new ExampleBottomSheetDialog();
bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");

// Check if a click count was set, then display the click count.
if (marker.equals(One)) {
    Toast.makeText(getApplicationContext(), "Balalaika: ",
            Toast.LENGTH_SHORT).show();
}

return false;

}

2 Answers2

0

you can implements the method GoogleMap.OnMarkerClickListener

@Override
public boolean onMarkerClick(Marker marker) {
    Log.e(TAG, "onMarkerClick: " + marker.getTitle() + " " + marker.getSnippet());


    return true;
}
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
0

I Customize my InfoWindows and write my on marker click event on

 mMap.setOnInfoWindowClickListener

And this code, work fine for me, maybe it can help you.

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //Initialize Google Play Services
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(false);
            mMap.getUiSettings().setMyLocationButtonEnabled(false);
        }
    } else {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(false);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);
    }
    addMarketToMap(mMap);

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker marker) {

            View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents, (FrameLayout) findViewById(R.id.map), false);

            TextView title = infoWindow.findViewById(R.id.title);
            title.setText(marker.getTitle());

       //customize infowindows

            return infoWindow;
        }

        @Override
        public View getInfoContents(Marker marker) {

            return null;
        }
    });

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
  //your marker Onclick event
        }
    });
}
Fereshteh Naji
  • 104
  • 1
  • 11