-2

I have some objects with markers. These objects have dynamic data and I would like to output them in the infowindow of the marker of the object. So far this is what I have:

function createRandomObjectWithAMarker() {
  var marker;
  var aData = "dataToDisplay";
  var infowindow = new google.maps.InfoWindow({
    content:callThisFunctionWhenOpenWindow(aData)
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });
  randomObject = {
    /*
    someData
    */
    marker: marker
  };
  return randomObject;
}

And I would like that this function to be called when I click the marker to show the return modifiedData as the content of the infoWindow.

function callThisFunctionWhenOpenWindow(aData){
  /*
  do some stuff on aData
  */
  return modifiedData;
}

But actually, this function is called once: only when I init the randomObject with the call of createRandomObjectWithAMarker(). So if I show the infoWindow after some time, when the datas would not be the same as when the script starter, it will still display the same output. Is there any way to do that? If yes how?

Kokodelo
  • 416
  • 1
  • 5
  • 25
  • Possible duplicate of https://stackoverflow.com/questions/28069881/multiple-infowindow-for-array-of-markers-on-google-maps – MrUpsidown Nov 28 '18 at 15:32
  • Possible duplicate of https://stackoverflow.com/questions/30012913/google-map-api-v3-add-multiple-infowindows – MrUpsidown Nov 28 '18 at 15:34
  • Possible duplicate of https://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example – MrUpsidown Nov 28 '18 at 15:35

1 Answers1

0

Try something like this?

This way the infowindow (and it's data) is only created when you click the marker

function createRandomObjectWithAMarker() {
  var marker;
  var aData = "dataToDisplay";
  marker.addListener('click', function() {
    var infowindow = new google.maps.InfoWindow({
      content:callThisFunctionWhenOpenWindow(aData)
    });
    infowindow.open(map, marker);
  });
  randomObject = {
    /*
    someData
    */
    marker: marker
  };
  return randomObject;
}
  • No, this is not what I would call "nice". This will create an InfoWindow object for each marker which is probably not what you want, especially if you have a large number of markers (+ it will force you to handle each InfoWindow separately). Create one InfoWindow object and use the `setContent()` method of the InfoWindow object to change its content. – MrUpsidown Nov 28 '18 at 15:30