【问题标题】:Loading Google Maps in jQuery tab在 jQuery 选项卡中加载谷歌地图
【发布时间】:2012-08-02 21:04:06
【问题描述】:

我在将 Google 地图加载到 jQuery 选项卡时遇到问题。我尝试了互联网上的每一个解决方案,使用这里的建议: Google Maps V3 not loading with jQuery tabs

Problems with Google Maps API v3 + jQuery UI Tabs

http://www.digitalwks.com/blogs/diogo-raminhos/google-maps-v3-and-jquery-tabs-jquery-tabs-google-maps-bug/

Google Maps and jQuery Tabs

等等……

但是它们都不起作用,要么我太菜鸟而无法使用它们,我不知道如何添加它们,要么不起作用。

这是标签的显示方式:

<div class="tabprice">

    <ul class="tabnavig">      
      <?php if ( $gmap_active ) { ?>
          <li><a href="#block2"><span class="big"><?php _e('Map', 'anuncios') ?></span></a></li>
      <?php } ?>
    </ul>

<?php if ( $gmap_active ) { ?>

    <!-- tab 2 -->
    <div id="block2">

        <div class="clr"></div>

            <div class="singletab">

                <?php include_once ( TEMPLATEPATH . '/includes/sidebar-gmap.php' ); ?>

            </div><!-- /singletab -->

    </div>

<?php } ?>

这里是sidebar-gmap.php的内容

http://pastebin.com/H3i4J8EN

求求你们,帮帮我吧!

【问题讨论】:

    标签: javascript jquery google-maps tabs rendering


    【解决方案1】:

    我是one of the blogposts you refered的作者。

    对于其他有同样问题的人:这里的问题取决于您需要在节目动画结束时触发调整大小事件。

    您可以通过调用 google.maps.event.trigger(map, "resize"); 在您的节目动画结束回调中做到这一点。

    因此,在您的情况下(对于链接 you sended),您需要:

    编辑以下文件:

    /wp-content/themes/anuncios/includes/js/theme-scripts.js

    并替换这个:

    /* Tab Control home main */
    jQuery(function($) {
        var tabContainers = $('div.tabcontrol > div');
        tabContainers.hide().filter(':first').show();
        $('div.tabcontrol ul.tabnavig a').click(function () {
            tabContainers.hide();
            tabContainers.filter(this.hash).fadeIn(100);
            $('div.tabcontrol ul.tabnavig a').removeClass('selected');
            $(this).addClass('selected');
            return false;
        }).filter(':first').click();
    });
    

    有了这个:

    /* Tab Control home main */
    jQuery(function($) {
        var tabContainers = $('div.tabcontrol > div');
        tabContainers.hide().filter(':first').show();
        $('div.tabcontrol ul.tabnavig a').click(function () {
            tabContainers.hide();
            tabContainers.filter(this.hash).fadeIn(100, function(){
                if(map != undefined && map != null)
                    google.maps.event.trigger(map, "resize");
            });
            $('div.tabcontrol ul.tabnavig a').removeClass('selected');
            $(this).addClass('selected');
            return false;
        }).filter(':first').click();
    });
    

    第 2 部分

    找到以下代码:

    $(tabs).click(function() {
        // hide all tabs
        $(tabContainers).hide().filter(this.hash).fadeIn(500);
        $(tabs).removeClass('selected');
        $(this).addClass('selected');
        return false;
    });
    

    然后替换为

    $(tabs).click(function() {
        // hide all tabs
        $(tabContainers).hide().filter(this.hash).fadeIn(500, function(){
            if(map != undefined && map != null)
                google.maps.event.trigger(map, "resize");
        });
        $(tabs).removeClass('selected');
        $(this).addClass('selected');
        return false;
    });
    

    您的错误将得到修复,这次我下载并在本地测试了它,所以我 100% 确定它会工作。

    【讨论】:

    • 哇,谢谢我的朋友。奥布里加多!我感谢您的帮助。我编辑了那部分,但正如你所见,没有任何改变。我仍然在那个文件中留下了你向我提出的更改,但问题仍然存在。
    • 嘿,我刚刚更新了我的答案,请查看 第 2 部分,我在本地对其进行了测试,它会起作用 ;)
    • 天啊,真的很管用。非常感谢你。可悲的是,您的网站上没有任何“给我买啤酒”按钮,因为您当然应该得到它。太棒了!
    【解决方案2】:

    Google 地图需要一个可见且固定的画布 (div) 来计算地图的宽度和高度,以便正确显示。如果你之前隐藏了你的div,你必须先显示它然后触发map resize google.maps.event.trigger(map, "resize");

    在您的情况下,在 calculate() 函数中调用 resize 似乎很理想

    function codeAddress() {
       google.maps.event.trigger(map, "resize");
    

    【讨论】:

    • 我在问题中添加的链接中看到了这一点。我尝试添加,但没有奏效。你能给我更多的信息吗?感谢您的回复。
    • 你能给我你的网址让我看看吗?或者只是在 jsfiddle 上做一些工作示例......
    • 嘿 Trinh,这是链接:pastebin.com/XsDAGhtG 我在这里添加它是因为我不希望它被 google 抓取。另外,我在上面的问题中包含了 2 个 php 文件。感谢您的帮助。
    【解决方案3】:

    我在 jquery 选项卡中有一份谷歌地图的工作副本http://jsfiddle.net/X5r8r/529/

    我使用了您上面列出的来源中的一些代码。这是执行此操作的代码。其他的只是地图的东西,不太重要

    $(文档).ready(函数() { //默认动作 $(".tab_content").css({'visibility':'hidden', 'position':'absolute'}); $("ul.tabs li:first").addClass("active").show(); $(".tab_content:first").css({'visibility':'visible' , 'position':'static'}); //点击事件 $("ul.tabs li").click(function() { $("ul.tabs li").removeClass("active"); $(this).addClass("active"); $(".tab_content").css({'visibility':'hidden', 'position':'absolute'}); var activeTab = $(this).find("a").attr("href"); $(activeTab).css({'visibility':'visible' , 'position':'static'}); 返回假; }); });​

    请记住,这仅适用于我在 jsfiddle 中显示的 div 标签,因为我检查了 div 中的 idclass 标签以激活选项卡。我只是基本上将 div 的可见性更改为隐藏或可见。

    希望这对您有所帮助。

    【讨论】:

    • 天哪……这个看起来很棒。我正在尝试在我的代码中实现它,但此时我得到的只是一个空白页而不是地图。我不能像你的代码那样完全改变它,但仍然在玩代码。你到底用什么来解决这个问题?标记? google.maps.event.addListener ?
    猜你喜欢
    • 2014-11-09
    • 2014-08-24
    • 2014-08-24
    • 1970-01-01
    • 2013-10-24
    • 2012-03-16
    • 2023-03-12
    • 1970-01-01
    • 2013-09-28
    相关资源
    最近更新 更多