【问题标题】:How to open all the infowindows as default when map loaded?加载地图时如何默认打开所有信息窗口?
【发布时间】:2017-12-23 03:17:59
【问题描述】:

一开始加载地图时,我一直在尝试打开所有标记的 infoWindows。然而“infoWindow.open(map, marker[i]);”或其他一些变化不起作用。即使我在其中尝试了另一个 for 循环,但无法做到。那你有什么建议吗?

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

【问题讨论】:

    标签: maps marker infowindow


    【解决方案1】:

    我终于找到了一个解决方案,但这是间接的方式。

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

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

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

    我希望 sn-p 可以帮助某人。

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

    谷歌来源在这里: https://developers.google.com/maps/documentation/javascript/examples/marker-labels

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-11
      • 2019-06-12
      • 1970-01-01
      • 2017-05-04
      相关资源
      最近更新 更多