Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    infowindow GMaps no iteration over text

    Hello, I wrote a GMaps JS Script which allows one to toggle different markers I fetch the data via php from a mySQL-Database:

    In order to open so-called infowindows I use G-Maps Event-Listeners. In the following script everything works just fine: the toggle function works. It' possible to open an info-windows on each marker.
    Howerver, I always get the the Infowindow-text of the last marker. So, there is no iteration over the text. I know that I always overwrite the content of the infowindow (until I have only the last item), so I dont' know how to fix it...
    I would be very grateful for just a hint. Thank you!!

    Code:
     var deutschland = new google.maps.LatLng(51,9);
      
    
         function initialize(){
    
          var mapOptions = { zoom: 6,
                             center: deutschland,
                             streetViewControl: true,
                             scaleControl: true,
                             mapTypeControl: true,
                             mapTypeId: google.maps.MapTypeId.ROADMAP };
    
    
       var map = new google.maps.Map(document.getElementById("map-container"),mapOptions);
      
    
       lat_data = [
    
    ['mid',50.0010400,9.0730700,'BluVisio','marker.png','infowindow text 1'],
    ['mid',50.2010057,8.2592738,'Alco GmbH','marker.png','infowindow text 2'],
    ['West',50.7430120,7.1264100,'Computer Wunsch Systems OHG','marker_green.png','infowindow 3 text'] ....
    
    ];
    
    
    
       markers = [];
       var i, newMarker;
    
       for (i = 0; i < lat_data.length; i++) {
    
           var shadow = new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow50.png',
           new google.maps.Size(37,32),
           new google.maps.Point(0,0),
           new google.maps.Point(0,32));
    
        var image = new google.maps.MarkerImage('http://maps.google.com/mapfiles/'+lat_data[i][4],
           new google.maps.Size(20,32),
           new google.maps.Point(0,0),
           new google.maps.Point(0,32));
    
         newMarker = new google.maps.Marker({
           position: new google.maps.LatLng(lat_data[i][1], lat_data[i][2]),
           map: map,
           title: lat_data[i][3],
           icon: image,
           shadow: shadow
         });
    
         newMarker.category = lat_data[i][0];
         newMarker.setVisible(true);
    
         var infowindow = new google.maps.InfoWindow({
               content: lat_data[i][5]
           });
    
           google.maps.event.addListener(newMarker,'click',function(){
             infowindow.open(map,this);
    
          });
          google.maps.event.addListener(map,'click',function(){
              infowindow.close(map,this);
          });
    
         markers.push(newMarker);
       }
    
       }
    
        function displayMarkers(category) {
          var i;
    
         for (i = 0; i < markers.length; i++) {
           if (markers[i].category == category) {
             if (markers[i].getVisible()) {
                  markers[i].setVisible(false); }
    
            else { markers[i].setVisible(true); } } }
    
       }

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    if you just want a hint, you should google "function closure" - there's plenty of stuff out there about getting closure for event listeners when using loops - it's not a gmaps problem, just a general javascript problem

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have been searching for a similar problem for the whole day.
    As I said, I think, that I overwrite infowindow.content over and over again.
    Is this true or false?

    edit: o.k., you are right, I just start to read the definition about closures ones again!!
    Last edited by michael_cassio; 02-13-2013 at 09:55 PM.

  • #4
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yo, I got it!!

    Code:
    ....
     iw = new google.maps.InfoWindow({
               content: lat_data[i][5]
           });
    
           function make_open_event_callback(iw){
               return function(){
                  iw.open(map,this);
               };
           }
            function make_close_event_callback(iw){
               return function(){
                  iw.close(map,this);
               };
           }
    
           google.maps.event.addListener(newMarker,'click',make_open_event_callback(iw));
          google.maps.event.addListener(map,'click',make_close_event_callback(iw));
    
         markers.push(newMarker);
    
    ....


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •