23

我正在开发一个使用 Google Maps api-v3 的项目,在地图上会有一些地方标记,其中包含我存储在 InfoWindow 中的信息。

我想知道您是否可以将 InfoWindow 设置为在页面加载时自动打开(即在没有用户交互的情况下自动打开)。

在网上搜索,我似乎发现它需要绑定到一个事件侦听器,但 InfoWindow 对象似乎拥有的所有事件都是鼠标事件。

有谁知道某种解决方法?

4

5 回答 5

32

不确定我是否完全理解您的问题,但这适用于我使用硬编码的 LatLng:

var infoWindow = null;
function initialize() 
{
    infoWindow = new google.maps.InfoWindow();
    var windowLatLng = new google.maps.LatLng(43.25,-68.03);
    infoWindow.setOptions({
        content: "<div>This is the html content.</div>",
        position: windowLatLng,
    });
    infoWindow.open(map); 
} // end initialize

google.setOnLoadCallback(initialize);
于 2012-04-27T00:38:10.737 回答
11

这个问题我很晚了,但我想分享一些东西。我也搜索了它的解决方案,甚至没有从上述解决方案中得到工作。然后我尝试了自己并得到了自己的解决方案(我不是专家,我的想法可能不好但对我来说很好)。我们通常喜欢

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

将上面的行替换为:

var infowindow = new google.maps.InfoWindow();
infowindow.setContent('some content here');
infowindow.open(map, marker);

不要等待标记的点击事件,只需设置内容并打开它(您必须定义地图和标记以及信息窗口指向您的标记)。

于 2012-08-09T21:21:49.600 回答
3

只需使用 infowindow.open(map,marker); 离开 google.maps.event.addListener。它应该如下所示:

      var marker = new google.maps.Marker({
      position: myLatlng1,
      map: map,
      title: 'Uluru (Ayers Rock)'

  });

  infowindow.open(map,marker);

  google.maps.event.addListener(marker, 'click', function() {
  });   
于 2014-05-26T15:46:23.463 回答
2

这是我的代码,它使用地图和鼠标悬停加载信息窗口。

map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
    map: map,
    icon: "/images/map-marker.png",
    draggable: true,
    position: results[0].geometry.location

});
var infowindow = new google.maps.InfoWindow({
    content: "Use pin to point your exact locality"
});
google.maps.event.addListener(marker, 'mouseover', function () {
    infowindow.open(map, marker);
});
infowindow.open(map, marker);
于 2015-04-09T09:46:57.437 回答
1

function initMap() {
    var cairo = {lat:18.519331, lng: 73.849421};
    var map = new google.maps.Map(document.getElementById('map'), {
        scaleControl: true,
        center: cairo,
        zoom: 15,
    });

    var infowindow = new google.maps.InfoWindow;
    infowindow.setContent('<b>adddress</b>');

    var marker = new google.maps.Marker({map: map, position: cairo});
    infowindow.open(map, marker);
    infoWindow.open(map); 
}
<script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCrCByzXY6y5KMSOi9AuqWVf4VYZPyJ5SE&language=hi&region=EG&callback=initMap">
</script>
<div id="map"></div>

于 2017-09-16T06:46:22.190 回答