Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android & Google Maps - close info window with back button

I have an activity that holds a fragment with Google Map view in it. App adds several dozens of markers to the MapView, using MarkerManager and ClusterRenderer to form clusters.

The problem is that when I have marker's InfoWindow opened and I press hardware Back button, it closes the app. Instead of that, I would like to have the InfoWindow closed.

Is there any straightforward way to achieve this?

like image 588
lookashc Avatar asked Jun 11 '26 17:06

lookashc


1 Answers

I managed to solve the problem.

I modified MarkerManager to send notification via EventBus when InfoWindow is about to be opened:

@Override
public View getInfoContents(Marker marker) {
    View content = fillContent();
    EventBus.getDefault().post(new MapInfoWindowShownEvent(marker));
    return content;
}

and I added event handling in the activity:

private Marker mLastShownInfoWindowMarker = null;

@Override
public void onBackPressed() {
    if(mLastShownInfoWindowMarker != null && mLastShownInfoWindowMarker.isInfoWindowShown()) {
        mLastShownInfoWindowMarker.hideInfoWindow();
    } else {
        super.onBackPressed();
    }
}

public void onEvent(MapInfoWindowShownEvent event) {
    mLastShownInfoWindowMarker = event.getMarker();
}
like image 69
lookashc Avatar answered Jun 14 '26 10:06

lookashc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!