0

Hey guys i'm trying to add multiple markers and infowindows to a map Here's code:

        while($wiersz=mysql_fetch_array($result)){
        $latit=$wiersz["Latitude"];
        $longit=$wiersz["Longitude"];
        $nick=$wiersz["Username"];
         ?>

        var lt = '<?php echo $latit ?>';
        var lng = '<?php echo $longit ?>'; 
        var nick = '<?php echo $nick ?>';
        var coords2 = new google.maps.LatLng(lt, lng);
        var marker = new google.maps.Marker({
                position: coords2,
                map: map,
         });

        var infowindow = new google.maps.InfoWindow({
         content:nick
           });

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

It creates marker but when i try to use infowindow only last one pops up. I tried making array infowindow as i saw the similar question but the map didn't even load.

John Smith
  • 844
  • 8
  • 26
  • Answer on link : http://stackoverflow.com/questions/23915353/multiple-markers-and-infowindows-on-google-maps-using-mysql/23915856#23915856 – HoangHieu May 28 '14 at 18:44
  • Yep i saw that question and it doesn't work. That's why i said that i tried approach with creating array of infowindows. – John Smith May 28 '14 at 18:50

1 Answers1

1

change your code to

var marker = new Array();
 var infowindow = new Array();
<?php $i = -1; 
while($wiersz=mysql_fetch_array($result)){
            $latit=$wiersz["Latitude"];
            $longit=$wiersz["Longitude"];
            $nick=$wiersz["Username"];
            $i++;
             ?>

            var lt = '<?php echo $latit ?>';
            var lng = '<?php echo $longit ?>'; 
            var nick = '<?php echo $nick ?>';
            var coords2 = new google.maps.LatLng(lt, lng);
            marker[<?php echo $i; ?>] = new google.maps.Marker({
                    position: coords2,
                    map: map,
             });

           infowindow[<?php echo $i; ?>] = new google.maps.InfoWindow({
             content:nick
               });

             google.maps.event.addListener(marker[<?php echo $i; ?>], 'click', function() {
             infowindow[<?php echo $i; ?>].open(map,this);
             });
             <?php  } ?>
HoangHieu
  • 2,802
  • 3
  • 28
  • 44