【问题标题】:Getting gMaps to center when opened in tabs在选项卡中打开时让 gMaps 居中
【发布时间】:2014-06-12 12:41:52
【问题描述】:

我在 wordpress 的选项卡中使用 gMaps。我可以显示地图,但是当单击选项卡时,地图不在其标记处居中。

gMap 代码

 jQuery(document).ready(function($) {
    $("#tb_gmap_<?php echo $id; ?>").gMap({
        maptype: "<?php echo $maptype; ?>",
        zoom: <?php echo $zoom; ?>,
        markers: [
            {
                address: "<?php echo $address; ?>",
                popup: <?php echo $popup; ?>,
                html: "<?php echo $html; ?>"
            }
        ],
        controls: {
            panControl: true,
            zoomControl: true,
            mapTypeControl: true,
            scaleControl: true,
            streetViewControl: false,
            overviewMapControl: false
        }
    });
});

HTML 重绘地图

<div id="tb_gmap_<?php echo $id; ?>" class="themeblvd-gmap" style="<?php echo $styles; ?>"></div>

jQuery 刷新地图

 // refresh google map
    tab_content.children('li[data-tab="' + data_tab + '"]').find('iframe[src^="https://www.google.com/maps"], iframe[src^="http://www.google.com/maps"]').each(function(){
        this.src = this.src;
        jQuery(this).css('opacity','0');
        jQuery(this).delay(400).animate({'opacity': '1'}, 500);
    });
    tab_content.children('li[data-tab="' + data_tab + '"]').find('.themeblvd-gmap').each(function(){
    google.maps.event.trigger(this, 'resize');
            });


    e.preventDefault();
});

请帮忙

【问题讨论】:

    标签: javascript jquery wordpress google-maps


    【解决方案1】:
      // put this after map get refreshed
    
      // give marker latitude and longitude here 
      map.setCenter(latitude,longitude);
    

    【讨论】:

    • 这篇文章被自动标记为低质量,因为它只是代码。您介意通过添加一些文本来解释它如何解决问题来扩展它吗?
    【解决方案2】:

    试试这个:

    // refresh google map
    tab_content.children('li[data-tab="' + data_tab + '"]').find('iframe[src^="https://www.google.com/maps"],  iframe[src^="http://www.google.com/maps"]').each(function () {
        this.src = this.src;
        jQuery(this).css('opacity', '0');
        jQuery(this).delay(400).animate({
            'opacity': '1'
        }, 500);
    });
    tab_content.children('li[data-tab="' + data_tab + '"]').find('.themeblvd-gmap').each(function () {
        var c = this.getCenter();
        google.maps.event.trigger(this, 'resize');
        this.setCenter(c);
    });
    e.preventDefault();
    

    【讨论】:

    • 谢谢,但这不起作用,因为我收到错误 var c = this.getCenter(); is an undefined function
    • 我建议你使用谷歌地图而不是 gMap。谷歌地图本身具有与 gMap 相同的功能和选项。此外,您可以轻松获取谷歌地图的文档和帮助。
    • 我正在使用谷歌地图。只是jquery文件是jquery.gmap.min.js
    • 好吧,谷歌地图与您在初始化时使用的 jquery.gmap.min.js 和 gMap 不同。 gMap 是谷歌地图的独立插件gmap.nurtext.de
    • 好的,感谢您的帮助。我需要为此使用另一个地图插件.. 谢谢
    猜你喜欢
    • 2018-12-05
    • 2012-04-25
    • 2016-10-12
    • 2020-09-02
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多