title: Google maps v3: Ajax -> Database -> InfoWindow -> Error
body: <p>I almost got it. Almost.</p>
  
  <p>After a couple of days trying to ajax data from database to infowindows.... , now all markers get the right infowindow... except the last entry in the database, which always get undefined.</p>
  
  <p>Here is the <a href="http://jsfiddle.net/Gloak/NMPRP/10/" rel="nofollow noreferrer">fiddle</a>.</p>
  
  <p>Why do I get undefined in just one entry? It looks like when the program is first loaded, it's empty, but after the first cycle, it is still undefined, for ever and ever...</p>
  
  <p>Here is the code, which is slightly different from fiddle because of Ajax. </p>
  
  <p>Any suggestion will be greatly appreciated. I have been doing computer for a few months and just to get this infowindow (almost) right, took me quite a few readings on ajax, closure and such. I have a vague idea why I had to create that parameter/variable "box" in the function dropmarker (...box), but it works, I mean, it almost works. If I don't use the <code>box.name</code>, but the <code>pos.lat ()</code>, then all is good. But I need the other values, because the final project will be a link to Youtube. I need to be able to bring all values. </p>
  
  <pre><code>var BERLIN = new google.maps.LatLng(-32.517683, -46.394393);
  var map = null;
  var marker = null;
  var index = 0;
  var infoWindow = [];
  var MY_MAPTYPE_ID = 'custom_style';
  
  
  
  function initialize() {
  
           var featureOpts = [ ]  // lots personal options deleted to shorten code
           var mapOptions = {} // lots personal options deleted to shorten code
          map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);
  
  
           var styledMapOptions = { name: 'Custom Style'
           };
  
          var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions);
  
          map.mapTypes.set(MY_MAPTYPE_ID, customMapType);
          function dropMarker (map,box, pos ){                                          return new google.maps.Marker({
                                  map: map,
                                  position: pos,
                                  draggable: false,
                                  }); // return
  
              //end of function
          }
  
  
          function changeMarker() {   
  
              if (marker) {
                  infoWindow.close();
                  marker.setMap(null);
  
              }
  
              var pos = neighborhood[index];
              var box = infowindows[index];
              marker = dropMarker(map,box, pos );
              var contentString = ('lat:' + box.lat+ '&lt;br /&gt;' + 'lng:' + box.lng  + '&lt;br /&gt;' + 'link:' +  box.link + '&lt;br /&gt;' +'name:' + box.name)
              infoWindow.setContent( contentString);
  
  
              setTimeout(function () {
                  infoWindow.open(map, marker);
              }, 200);
  
              index = (index + 1) % neighborhood.length;
  
              setTimeout(function () {
                  changeMarker();
              }, 3000);
      } 
  
              customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions); // this seems to be optional
              infoWindow = new google.maps.InfoWindow ()  
  
                  $.ajax({
                      type : 'POST',
                      url : 'php/locationsJson.php',
                      dataType : 'json',
  
                      success: function( json ){
                              neighborhood=[];
                              infowindows =[];
                              $.each(json,function(i,item){
                                  neighborhood.push(new google.maps.LatLng(this.lat,this.lng));
                                  infowindows.push(infoWindow)
                                  contentString = ({link:this.link,name:this.name,lat:this.lat,lng:this.lng})
                                  infoWindow = new google.maps.InfoWindow (contentString)     
                                  alert ( infowindows)                    
                           });    //$.each
  
               changeMarker();    
                      } // end of success
                  });//end of ajax        
  }  //end of initialized
  </code></pre>