简体   繁体   English

加载Google Maps控件元素时的回调

[英]Callback when Google Maps Controls Elements are Loaded

I found other stack overflow questions suggesting that using the 'idle' event is the way to trigger a callback when a google map is completely loaded. 我发现其他堆栈溢出问题,提示使用“ idle”事件是在Google地图完全加载时触发回调的方法。 But in my case, I want to run some code to inject some text into an element in the map's UI, but I find that 'idle' fires before these elements are created. 但就我而言,我想运行一些代码以将一些文本注入到地图UI中的元素中,但是我发现在创建这些元素之前会先触发“ idle”。

To be explicit, I attach a initialize function to the window's load method: 明确地说,我将初始化函数附加到窗口的load方法:

google.maps.event.addDomListener(window, 'load', initialize);

Inside initialize, I create some UI elements and attach them to the map: 在初始化内部,我创建了一些UI元素并将其附加到地图:

function initialize() {
  map = new google.maps.Map(document.getElementById("map-div"), mapOptions);
  ...
  // set up the controls div 
  var controlsDiv = document.createElement('div');
  controlsDiv.id="cd";
  controlSetup(controlsDiv);
  map.controls[google.maps.ControlPosition.RIGHT_TOP].push(controlsDiv);
}

Okay, but then what I want to do is run a function once this is all ready. 好的,但是当所有准备就绪后,我想做的就是运行一个函数。 In my case, I'm going to insert further text into the controls divs so I need those to be there. 就我而言,我将在控件div中插入更多文本,因此我需要这些文本。

I've tried this as the last line of the initialize method: 我已经尝试过将其作为initialize方法的最后一行:

google.maps.events.addListenerOnce(map,'idle',emitText);

However, the emitText has a line which tries to getElementById("cd") (the controlsDiv on the map) but I get that it is null because the emitText has fired before the controlsDiv has actually finished loading. 但是,emitText有一行尝试获取getElementById(“ cd”)(在地图上的controlsDiv),但是我得到它为null,因为在controlDiv实际完成加载之前就已经触发了sparkText。

The question, then, is how to detect the full loading of the map, including any UI divs. 然后,问题是如何检测地图的完整负载,包括任何UI div。 My testing suggests that the 'idle' event is firing before the UI elements are loaded and accessible in the DOM. 我的测试表明,在UI元素被加载并可以在DOM中访问之前,将触发“ idle”事件。 Thanks in advance! 提前致谢!

只需访问controlsDiv而不是getElementById("cd") ,就可以将div添加到文档中都没有关系。

I have same problem when i want run jquery plugin in side Element after add to #map. 当我想在添加到#map之后在side Element中运行jquery插件时遇到同样的问题。 So i solved with call back google map full load: 所以我解决了回叫谷歌地图满载:

google.maps.event.addListenerOnce(g, 'idle', () => {
        setTimeout(function() {
         //do smt
        }, 1)
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM