关闭Google Map的infowindow.open(map)异步行为 [英] Turn off google map's infowindow.open(map) asynchronous behaviour

查看:104
本文介绍了关闭Google Map的infowindow.open(map)异步行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个google maps应用程序,然后单击地图上的一个区域会创建信息窗口.

I have a google maps application, and clicking on a region on the map creates the infowindow.

这是我的代码的结构:

...
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)之后放置警报语句会导致通常执行最后一段代码.

The problem here is that the dom element manipulations at the end usually don't execute. My guess is that infowindow.open(map) is asynchronous, and that it usually only finishes executing after the dom element manipulation section, which don't execute since the the html elements don't exist yet. Placing an alert statement after infowindow.open(map) causes the last section of code to usually execute, assumingly due to the delay caused.

那么,如何确保只有在 infowindow.open()完成执行之后,任何代码才执行?我可以使其同步吗?还有另一种方法吗?我不只是想添加一个延迟函数,因为这可能会非常缓慢或不可靠.

So how can I make sure that any code only executes AFTER infowindow.open() has finished executing?
Can I make it synchronous? Is there another way to do this? I don't just want to add a delay function, since that would be either very slow or unreliable.

推荐答案

等待

准备就绪 |无|当包含InfoWindow的内容时,将触发此事件附加到DOM.如果您正在建立自己的系统,则不妨监视此事件信息窗口内容是动态的.

domready | None | This event is fired when the containing the InfoWindow's content is attached to the DOM. You may wish to monitor this event if you are building out your info window content dynamically.

代码:

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"});
});

这篇关于关闭Google Map的infowindow.open(map)异步行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆