Check if infowindow is opened Google Maps v3

隐身守侯 提交于 2019-11-29 02:13:33

问题


Please, I need a help.

I want to check if my infowindow is opened.

For example:

if (infowindow.isOpened)
{
   doSomething()
}

or

if (infowindow.close)
{
   doAnotherthing();
}

I dont have any idea, how to do this


回答1:


This is an undocumented feature, and is therefore subject to change without notice, however the infoWindow.close() method sets the map on the object to null (this is why infoWindow.open(map, [anchor]) requires that you pass in a Map), so you can check this property to tell if it is currently being displayed:

function isInfoWindowOpen(infoWindow){
    var map = infoWindow.getMap();
    return (map !== null && typeof map !== "undefined");
}

if (isInfoWindowOpen(infoWindow)){
    // do something if it is open
} else {
    // do something if it is closed
}

Update: Another potentially useful way to write this is to add an isOpen() method to the InfoWindow prototype.

google.maps.InfoWindow.prototype.isOpen = function(){
    var map = this.getMap();
    return (map !== null && typeof map !== "undefined");
}



回答2:


Until google doesn't give us any better way of doing this, you can add a property to the infoWindow objects. Something like:

google.maps.InfoWindow.prototype.opened = false;
infoWindow = new google.maps.InfoWindow({content: '<h1> Olá mundo </h1>'});

if(infoWindow.opened){
   // do something
   infoWindow.opened = false;
}
else{
   // do something else
   infoWindow.opened = true;
}



回答3:


I modified the prototype for google.maps.InfoWindow and changed open/close to set/clear a property:

//
// modify the prototype for google.maps.Infowindow so that it is capable of tracking
// the opened state of the window.  we track the state via boolean which is set when
// open() or close() are called.  in addition to these, the closeclick event is
// monitored so that the value of _openedState can be set when the close button is
// clicked (see code at bottom of this file).
//
google.maps.InfoWindow.prototype._open = google.maps.InfoWindow.prototype.open;
google.maps.InfoWindow.prototype._close = google.maps.InfoWindow.prototype.close;
google.maps.InfoWindow.prototype._openedState = false;

google.maps.InfoWindow.prototype.open =
    function (map, anchor) {
        this._openedState = true;
        this._open(map, anchor);
    };

google.maps.InfoWindow.prototype.close =
    function () {
        this._openedState = false;
        this._close();
    };

google.maps.InfoWindow.prototype.getOpenedState =
    function () {
        return this._openedState;
    };

google.maps.InfoWindow.prototype.setOpenedState =
    function (val) {
        this._openedState = val;
    };

You also need to monitor the closeclick event because clicking on the close button does not call close().

//
// monitor the closelick event and set opened state false when the close
// button is clicked.
//
(function (w) {
    google.maps.event.addListener(w, "closeclick", function (e) {
        w.setOpenedState(false);
    });
})(infowindow);

Calling InfoWindow.getOpenedState() returns a boolean which reflects the state (opened/closed) of the infowindow.

I chose to do it this way instead of the using the InfoWindow.getMap() or MVCObject.get('map') method because of the well known pitfalls of using undocumented behavior. However google uses MVCObject.set('map', null) to force the removal of the InfoWindow from the DOM, so it is unlikely that this will change...




回答4:


infowindow.getMap() returns null if infowindow is closed.

So you can use simply:

if (infowindow.getMap());



回答5:


You can simply set key and value for infoWindow: infoWindow.set('closed', true);

example:

const infoWindow = new google.maps.InfoWindow({
    content: 'foo',
    position: {
        lat: some_number,
        lng: some_number
    }
});

infoWindow.set('closed', true);

// Clicking on polyline for this example
// Can be marker too
polyline.addListener(
    'click',
    () => {
        if (infoWindow.get('closed')) {
            infoWindow.open(map);
            infoWindow.set('closed', false);
        } else {
            infoWindow.close();
            infoWindow.set('closed', true);
        }
    }
);


来源:https://stackoverflow.com/questions/12410062/check-if-infowindow-is-opened-google-maps-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!