2016-01-07 41 views
-1

该代码应该添加一个单击事件,其中一个infowindow用PHP页面中的数据在标记上打开,并且除了第一个infowindow打开空白在h1中说新闻),之后我打开的其他每个infowindow都包含最后一个标记的内容。任何人都可以告诉我如何解决它?

//broken part 
marker.addListener('click', function() { 
    $.getJSON('articles.php/GET?geo='+ marker.title) 
    .done(function(data, textStatus, jqXHR) { 
     $.each(data, function() { 
      contentString=contentString + '<li><a href=' + data[counter].link + '>' +data[counter].title + '</a></li>' ; 
      counter++; 
     });   
    }); 

    contentString=contentString + '</ul></div>' ; 
    var infowindow= new google.maps.InfoWindow({ 
     content: contentString 
    }); 

    infowindow.open(map, marker); 
    counter=0 ; 
    contentString='<div align="center"><h1>news</h1><br></div><div align="left" colour="blue"><ul>'; 
}); 

回答

0

$ .getJSON是异步的,你需要使用它的回调函数,何时/何地它是用(.done函数内)内的数据。

marker.addListener('click', function() { 
    $.getJSON('articles.php/GET?geo='+ marker.title) 
    .done(function(data, textStatus, jqXHR) { 
     $.each(data, function() { 
      contentString=contentString + '<li><a href=' + data[counter].link + '>' +data[counter].title + '</a></li>' ; 
      counter++; 
     });   
     contentString=contentString + '</ul></div>' ; 
     var infowindow= new google.maps.InfoWindow({ 
      content: contentString 
     }); 

     infowindow.open(map, marker); 
     counter=0 ; 
     contentString='<div align="center"><h1>news</h1><br></div><div align="left" colour="blue"><ul>'; 

    }); 
}); 
+0

很多thx的,这就是我如何初始化它,但它不断给我在页面上的错误,我想我只是有一些语法问题cuz工作正常。 –