如何在地图加载时打开所有infowindows作为默认值?

时间:2017-07-18 10:08:33

标签: maps marker infowindow

我一直试图在每次加载地图时打开所有标记的infoWindows。然而" infoWindow.open(map,marker [i]);"或其他一些变化不起作用。即使我在其中尝试了另一个循环但是无法完成它。所以你有任何建议吗?

var spLocations_length = spLocations.length;

// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(),
    marker, i;

var iconCounter = 0;
// Loop through our array of markers & place each one on the map
for (i = 0; i < spLocations_length; i++) {
    var position = new google.maps.LatLng(spLocations[i][1], spLocations[i][2]);
    bounds.extend(position);
    marker = new google.maps.Marker({
        position: position,
        map: map,
        title: spLocations[i][0],
        icon: icons[iconCounter]
    });
    // Allow each marker to have an info window
    google.maps.event.addListener(marker, 'click', (function (marker, i) {
        return function () {
            infoWindow.setContent(infoWindowContent[i][0]);
            infoWindow.open(map, marker);
        }
    })(marker, i));

   // IT DOESN'T WORK FOR OPEN ALL MARKERS INFOWINDOWS WHEN MAP LOADED
    infoWindow.open(map, marker[i]);



    // Automatically center the map fitting all markers on the screen
    map.fitBounds(bounds);
}

1 个答案:

答案 0 :(得分:0)

最后我发现了一个解决方案,但这是间接的方式。

相反,打开所有信息窗口,我使用了“标记标签”解决方案。

诀窍是使用“标记图标”图像作为背景而不是infowindow容器。

因此,标签文本通过for循环和数组动态变化。

我希望该片段可以帮助某人。

// locations Array contains location coords and titles //
var locations[.....]; 

for (i = 0; i < locations.lenght; i++) {

    var position = new google.maps.LatLng(locations[i][1], locations[i][2]);

    marker = new google.maps.Marker({
    position: position,
    map: map,
    title: locations[i][0],
    icon: "https.......",
    index: i,
    label: {
      text: locations.length + locations[i][0] "This is text",
      color: 'black',
      fontSize: "14px",
      fontFamily: "Roboto, Arial, sans-serif",
      fontWeight: "bold"
    }

  });
}

Google来源: https://developers.google.com/maps/documentation/javascript/examples/marker-labels