简体   繁体   中英

Hide infoWindow when clicked marker second time (Google Map Android API V2)

To achieve this I feel I simply need to override onMarkerClick and check if the infoWindow of clicked marker has already been open, but to my surprise clickedMarker.isInfoWindowShown() always return false...

My codes:

@Override
public boolean onMarkerClick(final Marker clickedMarker) {
    if(clickedMarker.isInfoWindowShown()) {
        clickedMarker.hideInfoWindow();
        System.out.println("was showing");
    } else {
        clickedMarker.showInfoWindow();
        System.out.println("not showing");
    }
}

It always tells me the infoWindow is "not showing" which is not right... I can get around this by keeping a reference of userLastClickedMarker but I'd like to know where I did wrong above.

Thanks!

Edit:

I changed my code to debug this issue:

    System.out.println(clickedMarker.isInfoWindowShown());
    if(clickedMarker.isInfoWindowShown()) {
        clickedMarker.hideInfoWindow();
        System.out.println(" showing");
    } else {
        clickedMarker.showInfoWindow();
        System.out.println("not showing");
    }
    System.out.println(clickedMarker.isInfoWindowShown());

I found that isInfoWindowShown() does work correctly in onMarkerClick method but won't 'remember' it correctly. And I found this has been pointed out as a bug by someone already...

https://code.google.com/p/gmaps-api-issues/issues/detail?id=5408

I had the same issue and I found a simple solution by declaring a global boolean variable and switching its value when user clicks on a marker. Here's my code:

private boolean isInfoWindowShown = false;

   map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {

            if (!isInfoWindowShown) {

                marker.showInfoWindow();
                map.moveCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
                isInfoWindowShown = true;
            } else {

                marker.hideInfoWindow();
                isInfoWindowShown = false;
            }

           return true;
        }
    });

Hope this will help somebody.

According to the documentation it is correct:

An info window allows you to display information to the user when they tap on a marker. Only one info window is displayed at a time. If a user clicks on another marker, the current info window will be hidden and the new info window will be displayed.

We may consider that as bug from usage perspective.

@Override
public boolean onMarkerClick(Marker marker)

From business logic this is not the proper place to call marker.isInfoWindowShown() function because it's always false as supposed to be. When OnMarkerClickListener call this function any opened InfoWindow have been already hidden by Map framework itself (so status 'false' technically is correct).

You can check that by example modifying overridden onMarkerClick method to open InfoWindows on even cliks only. On odd cliks - do nothing and see InfoWindows closes automatically by framework without explicit call to marker.hideInfoWindow() ;

To get actual InfoWindow status you should call marker.isInfoWindowShown from another events or functions by keeping local references to any markers on map.

isInfoWindowShown always false, so I hide info window using the basic way

int mLastIndex = -1

and on

@Override
public boolean onMarkerClick(Marker marker) {   
    int index = (int) marker.getTag();
    Log.i(TAG, "onMarkerClick: " + index);
    if(lastIndex != index){
        marker.showInfoWindow();
        lastIndex = index;
    }else{
        marker.hideInfoWindow();
        lastIndex = -1;
    }

    return true;
}

In my case I am showing my own window info upon google window info,

My window info

And I was need to hide google window info any time user clicks on other place eg map,buttons .

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    Marker lastMarkerClicked = modelManager.getLastMarkerClicked();
    if(lastMarkerClicked!=null){
        lastMarkerClicked.hideInfoWindow();
    }
    super.onWindowFocusChanged(hasFocus);
}

But I also need googles window info because one of my features is to share screenshot of map , and there window info is needed for the screenshot feature.

( For this to work The popup window should lose focus So its focusable property should be set to true )

I create this code for show/hide infowindow.

enter code here

private boolean infoWindowIsShow = false;
private Marker lastMarker;

private class MarkerClickListener implements GoogleMap.OnMarkerClickListener {
    @Override
    public boolean onMarkerClick(Marker marker) {

        //обработка выводить/не выводить инфовиндов
        if(lastMarker == null){
            marker.showInfoWindow();
            lastMarker = marker;
            infoWindowIsShow=true;
        }else
        if (marker.getId().equals(lastMarker.getId())) {
            if (infoWindowIsShow) {
                marker.hideInfoWindow();
                infoWindowIsShow = false;
            } else {
                marker.showInfoWindow();
                infoWindowIsShow = true;
            }
        }
        else{
            //это щелчок по другому маркеру
            if (infoWindowIsShow) {//если открыто инфовиндов предыдущего маркера, скрываем его
                lastMarker.hideInfoWindow();
                //и отображаем для нового
                marker.showInfoWindow();
                infoWindowIsShow = true;
                lastMarker = marker;
            } else {
                marker.showInfoWindow();
                infoWindowIsShow = true;
                lastMarker = marker;
            }
        }
        return true;
    }
}

mMap.setOnMarkerClickListener(new MarkerClickListener());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM