Google maps open info window by default?

Billa picture Billa · Nov 23, 2011 · Viewed 81.6k times · Source

The following example shows a simple Marker (standard icon) on Google maps for the location and when clicking the icon, it opens the info window. Can I show the info window open by default so that I don't need to click the icon to open?

var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
    var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var contentString = '<div id="content">'+
        '<div id="siteNotice">'+
        '</div>'+
        '<h2 id="firstHeading" class="firstHeading">Uluru</h2>'+
        '</div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title:"Uluru (Ayers Rock)"
    });

    google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
    });

Answer

Idham Perdameian picture Idham Perdameian · Nov 23, 2011

If you want to show the info window opened by default without click, just add a new code like bellow:

Your code:

google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
});

So we have a new code like this:

google.maps.event.addListener(marker, 'click', function() {
   infowindow.open(map,marker);
});

infowindow.open(map,marker);

As you can see, we just add a little line code with infowindow.open(map,marker);