自动打开Infowindow

时间:2015-12-23 02:17:15

标签: javascript jquery google-maps google-maps-api-3 jquery-gmap3

我正在为2个分支加载多个标记。我无法在加载页面时自动打开infowindow?

    var locations = [
  ['<strong>Info</strong><br /> Address', 40.004257, -105.253425, 2],
  ['<strong>Info</strong><br /> Address', 39.999326, -105.257662, 1]
];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 14,
  center: new google.maps.LatLng(40.00, -105.24),
  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]),
    icon: "images/favicon.png",
    map: map
  });

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

代码有什么问题吗?是否修复了自动打开信息窗口?

2 个答案:

答案 0 :(得分:0)

这是我在加载页面时加载infoWindows所做的工作。

function initMap() {
var locations = [
  ['<strong>Info</strong><br> Address', 40.004257, -105.253425, 2],
  ['<strong>Info</strong><br> Address', 39.999326, -105.257662, 1]
];

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

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
  });

  var infowindow = new google.maps.InfoWindow({
    content:locations[i][0]
  });
  infowindow.open(map, marker);
}
}

答案 1 :(得分:-1)

尝试添加

 infowindow.open(map,marker);
在for循环之后

也许这个可能有所帮助。

Google maps open info window by default?