2

I have a problem with Google Maps info windows. I have 2 markers and I want to open both info windows by default. But I can't figure out how to do it.

Here is my code:

<html>
<head>

<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=<key>&sensor=false"></script>
<script type="text/javascript">

var locations = [
    ['loan 1', 46.166621, 8.853315, '<div style="width:250px">text info 1'],
    ['loan 2', 46.173000, 8.856315, '<div style="width:250px">text info 2'],
];

function initialize() {
    var myOptions = {
        center: new google.maps.LatLng(46.166621, 8.853315),
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    };

    var map = new google.maps.Map(document.getElementById("default"), myOptions);
    setMarkers(map,locations);
}

function setMarkers(map,locations){
    var marker, i

    for (i = 0; i < locations.length; i++) {
        var loan = locations[i][0]
        var lat = locations[i][1]
        var long = locations[i][2]
        var add =  locations[i][3]

        latlngset = new google.maps.LatLng(lat, long);

        var marker = new google.maps.Marker({
            map: map, title: loan , position: latlngset
        });
        map.setCenter(marker.getPosition())

        var content = add
        var infowindow = new google.maps.InfoWindow()
        google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){
            return function() {
                infowindow.setContent(content);
                infowindow.open(map,marker);
            };
        })(marker,content,infowindow));
    }
}

</script>
</head>
<body onLoad="initialize()">
  <div id="default" style="width:100%; height:100%"></div>
</body>
</html>

I tried adding infowindow.open(map,marker); in various place but stil don't work.

poke
  • 369,085
  • 72
  • 557
  • 602

1 Answers1

0

Trigger the click-event for the markers.

Put this at the end of the for-loop:

google.maps.event.trigger(marker,'click',{});

working fiddle

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201