如何在Google地图页面加载中打开信息窗口

时间:2018-08-27 11:27:14

标签: javascript google-maps

我在Google地图中有一个标记。它有一个包含一些内容的信息窗口。我希望它应该在页面渲染不在click事件时自动打开。

   function initMap() {
        var myLatLng = new google.maps.LatLng(22.804270950051844, 86.18359432304942),
            myOptions = {
                zoom: 14,
                center: myLatLng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            },
            map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
            marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                icon: 'http://maps.google.com/mapfiles/ms/micons/green.png',
                label: "1",
            });
        var contentString = 'no 1';

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });
        marker.addListener('click', function () {
            infowindow.open(map, marker);
        });
         infowindow.open(map, marker);
        marker.setMap(map);
    }

1 个答案:

答案 0 :(得分:0)

不要放在侦听器中,而是放在主体initMap代码中

  function initMap() {
        var myLatLng = new google.maps.LatLng(22.804270950051844, 86.18359432304942),
            myOptions = {
                zoom: 14,
                center: myLatLng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            },
            map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
            marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                icon: 'http://maps.google.com/mapfiles/ms/micons/green.png',
                label: "1",
            });

        var contentString = 'no 1';

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

        infowindow.open(map, marker);

    }

,如果重叠有问题,请尝试分配一个合适的新职位,例如: (默认情况下,{position包含此信息窗口锚定对象的LatLng)

        var infowindow = new google.maps.InfoWindow({
            content: contentString,
            position: new google.maps.LatLng(22.80427+ 0.000005 , 86.18359+ 0.000005)
        });