单个Google地图标记的多个信息窗口

时间:2015-12-18 20:53:42

标签: javascript google-maps infowindow

有没有办法为单个谷歌地图标记创建多个infowindows?

例如,如果在一年中对同一地址进行多次交付,那么这些交付的属性是否可以在该标记的可滚动信息窗口中显示?

否则我最终会做一些非常脏的AJAX。

理论上,我很乐意创建一个contentStrings数组,如Google中的修改示例所示。理想情况下,这将在infowindow中显示为可导航的旋转木马。

function initMap() {
  var uluru = {lat: -25.363, lng: 131.044};
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: uluru
  });

  **var contentString = ['contentString1','contentString2','contentString3'];**

  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });

  var marker = new google.maps.Marker({
    position: uluru,
    map: map,
    title: 'Uluru (Ayers Rock)'
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });
}

1 个答案:

答案 0 :(得分:2)

单个标记的简单概念证明,在单击“下一个”按钮时遍历字符串数组:

function initMap() {
  var uluru = {
    lat: -25.363,
    lng: 131.044
  };
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: uluru
  });

  infowindow = new google.maps.InfoWindow({
    content: contentString[0] + "<br><input type='button' onclick='next(1);' value='next' />"
  });

  var marker = new google.maps.Marker({
    position: uluru,
    map: map,
    title: 'Uluru (Ayers Rock)'
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });
  map.addListener('click', function() {
    infowindow.close();
  });
}
var contentString = ['contentString1', 'contentString2', 'contentString3'];
var infowindow;

function next(i) {
  var next = i + 1;
  if (next >= contentString.length) {
    next = 0;
  }
  infowindow.setContent(contentString[i] + "<br><input type='button' onclick='next(" + next + ");' value='next' />")
}
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>