InfoWindow close() function is not working

I am trying to manage an arrray of InfoWindow objects from the Google Maps JavaScript API v3. My array is defined as global so I can manage all the Infowindow objects like this:

I edited the code to add some details as Sean told:

            google.maps.event.addListener(markers[id], 'click', function() {


                for ( var i = 0; i < infowindows.length; i++ ) {
                    var infoWin = infowindows[i];
                    if ( infoWin === undefined || infoWin === null ) {
                        continue;
                    }
                    infoWin.close();

                }

                map.panTo(markers[id].getPosition());
                infowindows[id].open(map,markers[id]);
            });

The .close() function is not doing anything. I know that I am accessing the objects in a proper way because I can call .open() and it works. Anyone have an idea about this issue? Thanks!

EDIT (14 / 8 / 2012): It seemed to be a problem with CSS, so I managed to fix it by forcing the display style when I showed the map with some JQuery:

$('#google-map').css("display", "block !important");    

Thanks a lot for your kind answers!

Answers


I started to add a comment, but I have multiple suggestions, so I'm adding a full answer for clarity. Here is my initial feedback, with one question:

  1. The JavaScript for - in loop is intended to be used to enumerate an object's properties and should generally be avoided when your intent is to iterate the contents of an Array. So, change the loop control to:

    for ( var i = 0; i < infowindows.length; i++ ) {

  2. There is no way to know for sure, but index within the for loop's definition does not include the var keyword, so it may be creating a global variable. If you refactor to use a traditional for loop, as suggested in #1, this possible problem is removed.

  3. There doesn't seem to be a need for the guard condition: if (typeof infowindows[index] == "object") {, so try removing the test altogether. If you know the array contains InfoWindow instances, this is unnecessary.

  4. The semicolon after the for loop's final closing brace, }, is unnecessary; I don't think it's doing any harm, but it should be removed.

Question: Is this code generating any errors?

Follow up to your comment:

You don't have to use for - in if you believe there will be null members in the array. If you have array members that have not been assigned, they will be undefined; if you have members that are null, it is because the code has set them to null. But in eather case, you can deal with those situations and still use array iteration:

for ( var i = 0; i < infowindows.length; i++ ) {
    var infoWin = infowindows[i];
    if ( infoWin === undefined || infoWin === null ) {
        //skips the remainder of the loop code and starts a new iteration
        continue;
    }

    infoWin.close();
}

Using for - in when your intent is loop iteration is a bad practice that works sometimes, but will eventually get you into trouble. It enumerates an object's properties and will even pull in properties from the prototype chain, so you are never entirely sure what you will be getting. Also, for - in may not enumerate the properties of the array in order, which can sometimes cause problems, because some code assumes that arrays are being traversed in order.

If you are using similar logic to call open() that is working well, I have to assume that code is making changes to the array between the two loops and doing something that is causing the looping where you call close() to fail. If you add some additional code to your question, it will be helpful.


Again, this is too long for a comment...

If infoWin.close() isn't closing the InfoWindow, the most obvious cause is that infoWin is not an InfoWindow.

            for ( var i = 0; i < infowindows.length; i++ ) {
                var infoWin = infowindows[i];
                if ( infoWin === undefined || infoWin === null ) {
                    continue;
                }
                infoWin.close();
            }

This loop iterates through an array of objects which you have called infowindows, and you test that infoWin is not undefined or null but that doesn't check that it's actually an InfoWindow.

Because .close() doesn't cause an error, whatever it is has a close() method. Only an InfoWindow has a getContent() method, so you could test for that as a positive indication that the object is actually an InfoWindow.

If it is an InfoWindow, then I think we'll need a link to your map. In that case, the close() method should work, so something must have changed that.


Need Your Help

Vertex Shader Particle Movement

math opengl-es vector-graphics vertex-shader

I want to move my particles by a few given attributes. The particles should slow down to a stop as they reach the end of their lifetime

Add content to PHP WP excerpt

php wordpress

I'm a complete PHP newbie and I need a solutionto get the most out of this stupid problem with a custom Wordpress theme.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.