zdebruine
zdebruine

Reputation: 3807

Multiple Infowindows for a Single Google Maps Marker

Is there a way to create multiple infowindows for a single google maps marker?

For example, if several deliveries are made to the same address throughout the year, can the properties for each of those deliveries be displayed in a scrollable infowindow from that marker?

Otherwise I would end up doing some really dirty AJAX.

In theory I would love to just create an array of contentStrings as shown in the modified example from Google. Ideally this would be displayed as a navigable carousel within the 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);
  });
}

Upvotes: 0

Views: 637

Answers (1)

geocodezip
geocodezip

Reputation: 161324

Simple proof of concept for a single marker, iterate through the array of strings when a "next" button is clicked:

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>

Upvotes: 2

Related Questions