简体   繁体   English

在显示jQuery Mobile页面之前正确加载Google地图

[英]Getting Google map to properly load before jQuery Mobile page is shown

Issue: 问题:
I have a dialog page in a jQuery Mobile document which contains a small Google map to show a location. 我在jQuery Mobile文档中有一个对话框页面,其中包含一个小的Google地图以显示位置。 I want to load the map before loading the dialog page. 我想在加载对话框页面之前先加载地图。 As in: 如:

$("#dialogPage").bind("pagebeforeshow", function() {
    initializeSmallMap();
});

However, Google Maps works incorrectly whenever the div the map is contained in is has a display proprety of none (see Google Maps Display:None Problem ). 但是,每当包含该地图的divdisplay属性为none时, Google Maps便无法正常工作(请参阅Google Maps Display:None Problem )。 And since this function is executed before the page is actually loaded, the containing div naturally has a display property of none. 并且由于此函数是在实际加载页面之前执行的,因此包含divdisplay属性自然为none。

Now, obviously, there's a simple solution to this. 现在,显然,有一个简单的解决方案。 All I need to do is initialize the map after the page has been shown, which makes the div have a display property of block: 我需要做的就是在显示页面后初始化地图,这使div具有block的display属性:

$("#dialogPage").bind("pageshow", function() {
    initializeSmallMap();
});

However, since the dialog has already been loaded, there is an annoying, noticeable flicker when the map appears. 但是,由于对话框已经加载,因此在显示地图时会出现令人讨厌的,明显的闪烁。

Question: 题:
How do I get rid of the described flicker? 如何摆脱所描述的闪烁?

Possible Solutions: 可能的解决方案:

  • Making the div containing the map have a display property of block before the dialog is loaded. 在加载对话框之前,使包含地图的div具有block的display属性。 If I wasn't using jQuery Mobile, it would be simple and painless to implement this effect while still hiding the map until needed: 如果我不使用jQuery Mobile,则实现此效果既简单又轻松,同时仍将地图隐藏直到需要时:

     position: absolute; left: -10000px; display: block; 

    However, since jQuery Mobile automatically overrides page styles, I don't know how I could get this to work. 但是,由于jQuery Mobile自动覆盖页面样式,所以我不知道如何使它起作用。

  • Explicitly telling the Google Maps API the size of the div containing the map. 明确告知Google Maps API包含地图的div的大小。 I Googled this option and had no luck, but considering my terrible Googling skills, I would not be completely surprised if this was actually possible. 我用谷歌搜索了该选项,但没有运气,但是考虑到我的谷歌搜索技巧,如果真的可行,我不会感到完全惊讶。
  • Others - I'm open to any ideas on how to get rid of the flicker effect. 其他人-关于如何消除闪烁效果,我持开放态度。 The above are just some possible solutions. 以上只是一些可能的解决方案。

  •  $("#dialogPage").promise().done(function() {
       //Code to execute when map is loaded.
     });
    

    You could try it out. 您可以尝试一下。 Just hide the element with the attr() and display it with attr() when promise().done is executed :) And you should be in no use of display properties. 只需使用attr()隐藏该元素,并在执行promise()。done时使用attr()进行显示即可:)而且,您不应使用display属性。

    Ref - Promise 参考- 承诺

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

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