简体   繁体   English

如何在没有点击事件的情况下在谷歌地图中显示信息窗口

[英]how to show info window in google map without click event

How to show infowindow on page load in google maps.如何在谷歌地图的页面加载时显示信息窗口。 It should automatically show the infowindow pop up.它应该自动显示信息窗口弹出。 I have multiple markers in the google map.我在谷歌地图中有多个标记。 I need all markers info window should open in page load.我需要在页面加载时打开所有标记信息窗口。

js fiddle link https://jsfiddle.net/vq7zfbgj/ js 小提琴链接https://jsfiddle.net/vq7zfbgj/

code:代码:

    var locations = [
          [
            "Market 1",
            22.809999,
            86.179999,
            5,
            "Market1",
            "Market1",
            "Market1",
            "found"
        ],
     [
            "Market 2",
            22.801111,
            86.171111,
            5,
            "Market2",
            "Market2",
            "Market2",
            "found"
        ]
    ]

    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 14,
        center: new google.maps.LatLng(22.803444, 86.179525),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();


    var marker, i;

    for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });

        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(locations[i][0], locations[i][6]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

You can open multiple markers at once by initiating InfoWindow separately for each marker.您可以通过为每个标记分别启动 InfoWindow 来一次打开多个标记。 I'd suggest removing the original var infowindow and doing the following within the for loop for each marker:我建议删除原始 var infowindow 并在 for 循环中为每个标记执行以下操作:

marker.infowindow = new google.maps.InfoWindow();
});
marker.infowindow.setContent(locations[i][0], locations[i][6]);              
marker.infowindow.open(map, marker);

https://jsfiddle.net/vq7zfbgj/16/ https://jsfiddle.net/vq7zfbgj/16/

I modified your fiddle to this and the infoWindow will open on page load.我修改你的小提琴这个和信息窗口会在页面加载打开。 I add another marker just to ilustrate that could resolve more markers.我添加另一个标记只是为了说明可以解决更多标记。 Hope it helps.希望能帮助到你。

for (i = 0; i < locations.length; i++) {
  var infowindow = new google.maps.InfoWindow;

  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function (marker, i) {
    return function () {
      infowindow.setContent(locations[i][0], locations[i][6]);
      infowindow.open(map, marker);
    }
  })(marker, i));

  infowindow.setContent(locations[i][0], locations[i][6]);
  infowindow.open(map, marker);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM