谷歌地图2 InfoWindows自动打开infoWindow,点击后1 infoWindow

时间:2015-12-10 18:24:45

标签: google-maps infowindow

我有一个自动打开的信息窗口。

我希望只有两个自动打开,而一个没有。效果就像在图片中一样。 我的代码:

identifierForVendor

Picture 1 = so now

Picture 2 = so it has to be

1 个答案:

答案 0 :(得分:0)

我建议你对它进行概括,在数组中添加一个成员以确定是否打开标记。

var locations = [
    // IW content, lat, lng, nowrap, open IW
    ['Oddział', 52.846190, 17.723237, 3, true],
    ['Oddział', 52.812224, 17.201023, 2, true],
    ['Zakład Poligraficzny - Siedziba', 52.847942, 17.757889, 1, false]       
];

然后这样打开infowindow:

if (locations[i][4]) {
  google.maps.event.trigger(marker, 'click');
}

proof of concept fiddle

代码段

function initialize() {
  var openedInfoWindow = [];
  var locations = [
    ['Oddział', 52.846190, 17.723237, 3, false],
    ['Oddział', 52.812224, 17.201023, 2, true],
    ['Zakład Poligraficzny - Siedziba', 52.847942, 17.757889, 1, true]
  ];
  var cityCircle;
  var map = new google.maps.Map(document.getElementById('map-canvas'), {
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    zoomControl: true,
    zoomControlOptions: {
      style: google.maps.ZoomControlStyle.SMALL
    }

  });

  var bounds = new google.maps.LatLngBounds();

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

    bounds.extend(marker.position);

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

    google.maps.event.addListener(marker, 'click', (function(marker, i, infowindow) {
      return function() {

        if (openedInfoWindow[i] != null) {
          openedInfoWindow[i].close();
          openedInfoWindow[i] = null;
        } else {
          infowindow.setContent(this.content);
          infowindow.open(map, this);
          openedInfoWindow[i] = infowindow;
          google.maps.event.addListener(infowindow, 'closeclick', function() {
            openedInfoWindow[i] = null;
          });
        }
      }
    })(marker, i, infowindow));

    if (locations[i][4]) {
      google.maps.event.trigger(marker, 'click');
    }
  }

  map.fitBounds(bounds);

  var listener = google.maps.event.addListener(map, "idle", function() {
    map.setZoom(9);
    google.maps.event.removeListener(listener);
  });
}

function loadScript() {
  var script = document.createElement('script');
  script.src = 'https://maps.googleapis.com/maps/api/js?' + 'callback=initialize';
  document.body.appendChild(script);
}

window.onload = loadScript;
html,
body,
#map-canvas {
  height: 100%;
  margin: 0;
  padding: 0;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>