标记需要点击两次才能从 ajax 通话中获取正确的信息

Marker requires two clicks to get the right info from ajax call

我正在使用 Googlemaps API。 我正在尝试使用 ajax 调用更新标记的信息窗口中的响应,但需要单击两次才能使用以下代码获取内容:

    google.maps.event.addListener(marker, 'click', (function(marker, i) 
            {
            //infowindow.setContent("Loading...");
            return function() {

            var abb = locations[i][3];
            var line = locations[i][4];
            var to1 = locations[i][5];

            //console.log(abb);

            getTimes(abb, line, to1, function(result) {
                html1 = "";

                html1 += result;

                //alert(html1);
                //console.log(html1);

            });
            //console.log(html1);
            infowindow.setContent(locations[i][0] + "<br>" + html1);
            infowindow.open(map, this);

            }
            }) (marker, i));

有人知道解决这个问题的方法吗?

谢谢。

getTimes函数是异步的,第一次点击只是请求响应,信息窗口没有任何内容,所以打不开。当您第二次单击时,内容将被缓存,并且可用并打开信息窗口。

在回调函数中打开信息窗口。

google.maps.event.addListener(marker, 'click', (function(marker, i) {
  return function() {
    var abb = locations[i][3];
    var line = locations[i][4];
    var to1 = locations[i][5];

    getTimes(abb, line, to1, function(result) {
      html1 = "";
      html1 += result;
      infowindow.setContent(locations[i][0] + "<br>" + html1);
      infowindow.open(map, this);
    });
  }
}) (marker, i));