默认情况下打开infowindows

时间:2014-11-30 17:41:21

标签: javascript google-maps

我的Google地图信息窗口有问题。我有2个标记,我想默认打开两个信息窗口。但我无法弄明白该怎么做。

这是我的代码:

<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>

我尝试在各个地方添加infowindow.open(map,marker);,但stil不起作用。

1 个答案:

答案 0 :(得分:0)

触发标记的点击事件。

将它放在for循环的末尾:

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

working fiddle