使用AJAX将内容加载到我的infowindow中的最佳方式是什么? 现在我正在使用iframe获得类似的效果,但我并不那么满意。 我认为这将是直接的,但由于某种原因令我困惑。 这就是它的工作现在:谷歌地图V3:通过AJAX加载infowindow内容
var markers = [];
var infowindow = new google.maps.InfoWindow();
$.each(JSON.parse(aulas), function(i, a){
var latlng = new google.maps.LatLng(a.aula.lat, a.aula.lng);
var marker = new google.maps.Marker({
position : latlng,
title: a.aula.title
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
infowindow.setContent("<div class='infowindow_content'><iframe src='aulas/show/" + a.aula.id + "'></iframe</div>");
infowindow.open(map, marker);
});
markers.push(marker);
});
人很容易就在infowindow.setContent调用之前抢到通过AJAX的内容,但我想要的信息窗口打开的时候才使Ajax调用。有什么想法吗?
顺便说一句:我正在使用jQuery。
正如答案中的建议,我决定将调用移动到setContent并打开到一个单独的函数。对于那些有兴趣的是解决了这个代码是:
function load_content(marker, id){
$.ajax({
url: 'aulas/show/' + id,
success: function(data){
infowindow.setContent(data);
infowindow.open(map, marker);
}
});
}
然后改变监听器:
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
load_content(marker, a.aula.id);
});
markers.push(marker);
});
让我知道如果我需要澄清我的回答:) – RedBlueThing 2010-08-02 06:38:53
不,那是完美的。我的想法变得混乱了,我只需要轻轻地向正确的方向推动。谢谢! – mikewilliamson 2010-08-02 22:47:05
没问题:)很高兴帮助。 – RedBlueThing 2010-08-02 23:39:11