-1

I am using Google Maps API, I would like to show an infowindow for every marker.

But the problem is the same content is showing for every marker infowindow. i.e., the last string of an array is showing in infowindow even though I have used function bindInfoWindow() it is not working:

I am using Google Maps API, I would like to show an infowindow for every marker.

But the problem is the same content is showing for every marker infowindow.i.e., the last string of an array is showing in infowindow even though I have used function bindInfoWindow() it is not working:

var poly;
var map;
var infodatajson = [
  17.4172192, 78.401285,
  17.418407, 78.401629,
  17.418090, 78.400610,
  17.417865, 78.398234
];

// for center the map   
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 15,
    center: {
      lat: 17.4172192,
      lng: 78.401285
    }
  });

  // for polyline
  var flightPlanCoordinates = new Array();

  for (i = 0; i < infodatajson.length; i += 2) {
    var point = new google.maps.LatLng(infodatajson[i], infodatajson[i + 1]);
    flightPlanCoordinates.push(point);
  }

  poly = new google.maps.Polyline({
    path: [new google.maps.LatLng(17.4172192, 78.401285),
      new google.maps.LatLng(17.418407, 78.401629),
      new google.maps.LatLng(17.418090, 78.400610),
      new google.maps.LatLng(17.417865, 78.398234)
    ],
    geodesic: true,
    strokeColor: '#0000FF',
    strokeOpacity: 1.0,
    strokeWeight: 3,
  });

  var infowindow;
  var infodata;
  var marker;

  // for markers
  for (var i = 0, ln = infodatajson.length; i < ln; i += 2) {

    infodata = infodatajson[i] + ", " + infodatajson[i + 1];

    marker = new google.maps.Marker({
      position: new google.maps.LatLng(infodatajson[i], infodatajson[i + 1]),
      map: map,
      title: infodatajson[i] + ", " + infodatajson[i + 1],
    });

    infowindow = new google.maps.InfoWindow({});

    //function bindInfoWindow(marker, map, infowindow, infodata) {           
     marker.addListener('click', function() {
     infowindow.setContent(infodata);
     infowindow.open(map, this);
   });
    //}
  }

  poly.setMap(map);
}
Naveen
  • 45
  • 3
  • 10
  • yes i have invoked **bindInfoWindow(marker, map, infowindow, infodata)** eventhough its not working – Naveen Nov 04 '17 at 08:58

1 Answers1

0

instead of your bindInfoWindow() function, use this

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