Answer the question
In order to leave comments, you need to log in
What is the error in assigning an action when clicking on a Google Maps Marker?
Only on the third or fourth time, an action is triggered that works when you click on a specific marker in Google Maps. In connection with what is this happening? It seems that there are no problems in the code. Is there any conflict possible? Code itself:
Main.java
public void onMapReady(GoogleMap googleMap) {
...
Lenin = mMap.addMarker(new MarkerOptions()
.position(LatLng));
Lenin.setTag(0);
mMap.setOnMarkerClickListener(this);
}
public boolean onMarkerClick(final Marker marker) {
ExampleBottomSheetDialog bottomSheet = new ExampleBottomSheetDialog();
bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
if (marker.equals(Lenin)) {
Toast.makeText(getApplicationContext(), "Кот: ",
Toast.LENGTH_SHORT).show();
}
return false;
}
public class ExampleBottomSheetDialog extends BottomSheetDialogFragment {
private BottomSheetListener mListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
Button button1 = v.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return v;
}
public interface BottomSheetListener {
void onButtonClicked(String text);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (BottomSheetListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement BottomSheetListener");
}
}
}
Answer the question
In order to leave comments, you need to log in
The link to Lenin must be saved to a global variable if you are going to work with it further. But it is better to tie to the tags to the marker and then check them. . But this hardcode approach is also bad. Usually in such cases there is an array with ready-made data, such as finding an object with the marker id in it and taking the text field from there and placing it in the balloon. marker.getTag() === "lenin"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question