【问题标题】:Turn off google map's infowindow.open(map) asynchronous behaviour关闭谷歌地图 infowindow.open(map) 异步行为
【发布时间】:2014-05-22 23:37:38
【问题描述】:

我有一个谷歌地图应用程序,点击地图上的一个区域会创建信息窗口。

这是我的代码结构:

...
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(MapArea, "click", showTB);


function showTB () {
    ...
    contentElement = //complicated table with buttons etc

    infowindow.setContent(sContentString);
    infowindow.setPosition(event.latLng);
    infowindow.open(map);

    //contentElement dom element manipulations, such as:
    $("#table_row").css({background: "#8af"});
}

这里的问题是最后的 dom 元素操作通常不会执行。我的猜测是infowindow.open(map) 是异步的,并且它通常仅在 dom 元素操作部分之后才完成执行,由于 html 元素尚不存在,因此不会执行。 在infowindow.open(map) 之后放置一条警报语句会导致最后一段代码通常执行,假设是由于延迟造成的。

那么我如何确保任何代码仅在 infowindow.open() 完成执行后执行?
我可以让它同步吗? 还有另一种方法可以做到这一点吗?我不只是想添加延迟功能,因为那样会非常慢或不可靠。

【问题讨论】:

标签: javascript jquery google-maps asynchronous


【解决方案1】:

等待infowindow 上的“domready”事件

准备就绪 |无 |当包含 InfoWindow 的内容时触发此事件 附加到 DOM。如果您正在构建您的 信息窗口内容动态。

代码:

infowindow.setContent(sContentString);
infowindow.setPosition(event.latLng);
infowindow.open(map);
google.maps.event.addListener(infowindow,'domready', function() {
  //contentElement dom element manipulations, such as:
  $("#table_row").css({background: "#8af"});
});

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    相关资源
    最近更新 更多