-1

My problem is that all of the infowindows made have the same content (the last array object). None of the answers I find seem to work for me.

function addMapMarkers(data) {

removeMarkers();

for(i=0; i<data.length; i++){
 var location = {lat: + data[i].koordinatLatLng[0], lng: + data[i].koordinatLatLng[1]};

 var marker = new google.maps.Marker({
   position: location,
   map: map,
   clickable: true
 });

 marker.info = new google.maps.InfoWindow({
   content: "Navn: " + data[i].navn
 });

 google.maps.event.addListener(marker, 'click', function() {
   marker.info.open(map, this);
 });


 map.markers.push(marker);
 };
};
Vertex
  • 1

2 Answers2

1

Or if you don't want to use more closures, you can just use the let keyword

let marker = new google.maps.Marker({
    position: location,
    map: map,
    clickable: true
})
Anatolii Suhanov
  • 2,524
  • 1
  • 12
  • 14
0

you should use a closure

  function addMapMarkers(data) {

  var addListenerOnPoint = function(actMark, actNavn){
       $('.gm-style-iw').prev('div').remove();

      var actContent = '<div><strong>' +actNome + 
                      '</strong><br/>' + actDescrizione + '<div>';

      var aInfowindow =  new google.maps.InfoWindow({
        content: "Navn: " + actNavn;
      });

      actMark.addListener('click', function() {
          aInfowindow.open(map, actMark);
      });

  }
  removeMarkers();

  for(i=0; i<data.length; i++){

   var location = {lat: + data[i].koordinatLatLng[0], lng: + data[i].koordinatLatLng[1]};

   var marker = new google.maps.Marker({
     position: location,
     map: map,
     clickable: true
   });

   addListenerOnPoint(marker,data[i].navn);


   map.markers.push(marker);
   };
  };
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107