Hide/show infoboxes?

4,009 views
Skip to first unread message

3D-kreativ

unread,
Aug 18, 2011, 2:14:59 PM8/18/11
to Google Maps JavaScript API v3
Hi,

I have just discovered Google Maps plugin Infobox. I have used the
example code at http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html
and I have added a loop to show several markers and infoboxes, but I
have one problem that I can't solve on my own and I hope to get some
help here.

First I wonder how to hide the infoboxes from the start and second, I
wonder how to hide previous infoboxes when click on a new marker?

My example code can be seen at http://fredagsmat.se/infobox.html

Thanks! :)

JFrancis

unread,
Aug 18, 2011, 2:30:34 PM8/18/11
to Google Maps JavaScript API v3
Hiding the previously-displayed Info Window when a different marker is
clicked looks something like this:

if (infowindow) {
infowindow.close();
infowindow = new google.maps.InfoWindow();
} else {
infowindow = new google.maps.InfoWindow();
}

IF (the variable) "infowindow" is TRUE (meaning an Info Window is
currently being displayed), THEN first close it, and then open a (new)
Info Window.
Otherwise, open a new Info Window.

HTH

JF

3D-kreativ

unread,
Aug 18, 2011, 2:46:08 PM8/18/11
to Google Maps JavaScript API v3
Thanks! I made a quick test, but I didn't got it to work!? Perhaps I
have done something wrong. Where should this code be placed in my
code? I'm aware that I have to change some code. Will this also solve
the issue with the infoboxes that are visible from start?

JFrancis

unread,
Aug 18, 2011, 4:07:58 PM8/18/11
to Google Maps JavaScript API v3
In my case, it is within my geocode code. The address is searched, the
marker appears, and an infoWindow opens. I did it this way because I
also have markers on the map which are created using a KML file, and
before implementing this change, I would get 2 open infoWindows, one
for the geocoded marker and one for a KML marker. It also closes the
geocoded infoWindow if a new address is searched and the previous
infoWindow is still open.

I'm thinking the code should be within the marker listener; something
like:

google.maps.event.addListener(marker, "click", function (e) {
if (ib) {
ib.close();
ib.open(theMap, this);
} else {
ib.open(theMap, this);

}
});

Don't quote me on that, though! >;-)

JF

Esa

unread,
Aug 19, 2011, 11:42:02 AM8/19/11
to Google Maps JavaScript API v3
I would recommend creating just a single instance of InfoBox. Use the
setContent() method in the click handler.
Message has been deleted
Message has been deleted

3D-kreativ

unread,
Aug 19, 2011, 2:58:12 PM8/19/11
to Google Maps JavaScript API v3
Sounds interresting, could you please explain a little bit more how I
can change my code to just create a single instance of the InfoBox?
And what else do I have to do with the code to get the setContent() to
work? Preciate som helping lines of code? I wrote this messages a
second time because the first reply disapeard! Thanks!

Esa

unread,
Aug 19, 2011, 3:12:43 PM8/19/11
to Google Maps JavaScript API v3
The last two lines inside your createMarkers() function are:

var ib = new InfoBox(myOptions);
ib.open(theMap, marker);

You can change them to

ib.setContent(boxText);
ib.open(theMap, marker);

if you first declare in global scope (outside that function)

var ib = new InfoBox(myOptions);

3D-kreativ

unread,
Aug 19, 2011, 3:20:48 PM8/19/11
to Google Maps JavaScript API v3
Thanks for the help and your patience, but despite your instruction,
it isn't working! There is no markers on the map? What could be wrong?

geoco...@gmail.com

unread,
Aug 21, 2011, 2:05:29 AM8/21/11
to Google Maps JavaScript API v3
On Aug 19, 12:20 pm, 3D-kreativ <p...@telia.com> wrote:
> Thanks for the help and your patience, but despite your instruction,
> it isn't working! There is no markers on the map? What could be wrong?

This works for me in Firefox and Chrome, but not IE8

http://www.geocodezip.com/fredagsmat_se_infoboxA.html

IE(8) doesn't seem to like this line in infobox.js (645):
this.div_.style.zIndex = index;

And got me as to why the markers don't show up without the:
ib.open(theMap, marker);
at the end.


-- Larry

3D-kreativ

unread,
Aug 21, 2011, 5:51:36 AM8/21/11
to Google Maps JavaScript API v3
It's working now after some adjustment in Firefox and IE 8, but not in
Safari!? I haven't test it in Chrome, I think it will work there. But
I would be extra happy if someone could
tell me why it isn't working in Safari? Thanks!

The latest example
http://fredagsmat.se/infobox3.html

Pil

unread,
Aug 21, 2011, 7:00:48 AM8/21/11
to Google Maps JavaScript API v3


On Aug 21, 11:51 am, 3D-kreativ <p...@telia.com> wrote:

> I would be extra happy if someone could
> tell me why it isn't working in Safari? Thanks!


The intended array elements are not automatically pushed into the
array literals. So all ot them are empty in Webkit browsers.
You have two choices: Either this one

var name=[];
if (typeof name == "object") {
name.push('Test 1');
name.push('Test 2');
name.push('Test 3');
}

or the cleaner and shorter solution:

var name= [
'Test 1',
'Test 2',
'Test 3' ];

Message has been deleted

3D-kreativ

unread,
Aug 21, 2011, 3:30:38 PM8/21/11
to Google Maps JavaScript API v3
Thanks, but none of this works! Not in Firefox and not in Safari! Are
you sure this solution works? There has never been any problem with
name.push('test 1') before in Safari, it has always worked with the
map. But when I use InfoBox, then it's not working with Safari. What
could be wrong?

On 21 Aug, 13:00, Pil <wolf...@gmail.com> wrote:

Pil

unread,
Aug 22, 2011, 2:56:38 AM8/22/11
to Google Maps JavaScript API v3
Doesn't seem that it has something to do with infobox. I tested the
following in a document without any further scripts:


<script type="text/javascript">

var name = [];
name.push('Test 1');
alert(name.length);

</script>


In Chrome (13.0) I get the following error:

Uncaught TypeError: Object has no method 'push'

In Safari (5.0.5) it's this error:

TypeError: 'undefined' is not a function (evaluating 'name.push('Test
1')')

No alert occurs in both browsers.

Seems like a bug in Webkit generally.

Rossko

unread,
Aug 22, 2011, 4:43:39 AM8/22/11
to Google Maps JavaScript API v3
> var name = [];

'name' is perhaps risky, does it still fail using 'banana'?

Will it work if declared using var name = new Array() ?

Pil

unread,
Aug 22, 2011, 6:17:51 AM8/22/11
to Google Maps JavaScript API v3


On Aug 22, 10:43 am, Rossko <ros...@culzean.clara.co.uk> wrote:
> > var name = [];
>
> 'name' is perhaps risky, does it still fail using 'banana'?

Rossko you are right.

Although 'name' doesn't belong the reserved words or avoidable
identifiers it shouldn't be used in Webkit browsers as a variable
name.

Don't use any of these words that can be found here. See especially
the "Global Properties and Methods".

http://javascript.about.com/library/blreserved.htm

(This was the second time I had such a problem with Webkit. I've now
bookmarked this page.)

3D-kreativ

unread,
Aug 22, 2011, 12:09:45 PM8/22/11
to Google Maps JavaScript API v3
Banana was much better!! Now the code is working fine again! I was
confused beacause push had worked before, but I remembered that I
changed the code from push.names to push.name and therefore didn't
work in chrome and safari. Thanks for the help!
Reply all
Reply to author
Forward
0 new messages