Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 857
Arch1tect Avatar asked Jun 17 '13 18:06

Arch1tect


People also ask

How do I get rid of InfoWindow in Google Maps?

After constructing an InfoWindow, you must call open to display it on the map. The user can click the close button on the InfoWindow to remove it from the map, or the developer can call close() for the same effect.

How do I put InfoWindow on marker?

Call setPosition() on the info window, or. Attach the info window to a new marker using the InfoWindow. open() method. Note: If you call open() without passing a marker, the InfoWindow will use the position specified upon construction through the InfoWindowOptions object literal.

How do I edit InfoWindow on Google Maps?

You can modify the whole InfoWindow using jquery alone... var popup = new google. maps. InfoWindow({ content:'<p id="hook">Hello World!


2 Answers

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;
}
like image 111
TapulaRasa Avatar answered Oct 03 '22 09:10

TapulaRasa


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.

like image 41
fregomene Avatar answered Oct 03 '22 10:10

fregomene