0

I want to add multiple markers and infowindows that activated with a click. I am having trouble to present the relevant infowindow of the marker, after a click the same infowindow open for all markers.

The information comes from a for loop this is the code

 function GetMarkerSuccess(results) {
        results = $.parseJSON(results.d);
        markers = [];
        infowindows = [];

        for (var i = 0; i < results.length; i++) {
            var myLatLng = { lat: results[i].Latitude, lng: results[i].Longitude };

            var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                icon:'images/car.png'
            });
            var infowindow = new google.maps.InfoWindow({
            content: "<div style='text-align:right'><h3>" + results[i].Date.substring(0, 10) + "</h3><p> התחלה: " + results[i].Starttime + " סיום: " + results[i].Endtime + "</p><p> &#8362  מחיר:" + results[i].Price + "</p></div>"
            });
            markers.push(marker);
            infowindows.push(infowindow);
            listenMarker(marker);
            function listenMarker(marker) {

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

        }
    }
Omar
  • 32,160
  • 9
  • 67
  • 108
Liav Nave
  • 43
  • 3

1 Answers1

1
     function GetMarkerSuccess(results) {
            results = $.parseJSON(results.d);
            markers = [];
            infowindows = [];

            for (var i = 0; i < results.length; i++) {
                var myLatLng = { lat: results[i].Latitude, lng: results[i].Longitude };

                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    icon:'images/car.png'
                });
                var infowindow = new google.maps.InfoWindow({
                content: "<div style='text-align:right'><h3>" + results[i].Date.substring(0, 10) + "</h3><p> התחלה: " + results[i].Starttime + " סיום: " + results[i].Endtime + "</p><p> &#8362  מחיר:" + results[i].Price + "</p></div>"
                });
                markers.push(marker);
                infowindows.push(infowindow);
                listenMarker(marker);
                function listenMarker(marker) {
               google.maps.event.addListener(marker,'click',     (function(marker,content,infowindow){ 
                    return function() {
                    infowindow.setContent(content);
                    infowindow.open(map,marker);
                };
                })(marker,content,infowindow));
                }
            }
        }
Umair Khan
  • 280
  • 3
  • 13