【问题标题】:Google Maps Resize not working谷歌地图调整大小不起作用
【发布时间】:2014-02-20 05:32:14
【问题描述】:

我在(最初隐藏的)选项卡中使用 Google 地图,地图是根据 Wordpress 高级自定义字段插件提供的示例代码生成的。

我的地图打开时缺少图块。我知道这是因为该选项卡最初是隐藏的,并且有各种使用 google.maps.event.trigger(map, "resize") 的解决方案,但对于我来说,我无法让它工作。

这是我找到的带有基本修正的 acf 代码,但仍然无法正常工作:

        (function($) {
            var map;
            function render_map( $el ) {
                var $markers = $el.find(".marker");
                var args = {
                    zoom        : 16,
                    center      : new google.maps.LatLng(0, 0),
                    mapTypeId   : google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map( $el[0], args);
                map.markers = [];
                $markers.each(function(){
                    add_marker( $(this), map );
                });
                center_map( map );
            }

            function add_marker( $marker, map ) {
                var latlng = new google.maps.LatLng( $marker.attr("data-lat"), $marker.attr("data-lng") );
                var marker = new google.maps.Marker({
                    position    : latlng,
                    map         : map
                });
                map.markers.push( marker );
                if( $marker.html() )
                {
                    var infowindow = new google.maps.InfoWindow({
                        content     : $marker.html()
                    });
                    google.maps.event.addListener(marker, "click", function() {
                        infowindow.open( map, marker );
                    });
                }
            }

            function center_map( map ) {
                var bounds = new google.maps.LatLngBounds();
                $.each( map.markers, function( i, marker ){
                    var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
                    bounds.extend( latlng );
                });

                if( map.markers.length == 1 )
                {
                    map.setCenter( bounds.getCenter() );
                    map.setZoom( 16 );
                }
                else
                {
                    map.fitBounds( bounds );
                }

            }
            $(document).ready(function(){
                $(".acf-map").each(function(){
                    render_map( $(this) );
                });
            });
            $(document).on("click", "#map-tab", function() {
                alert("clicked");
                google.maps.event.trigger(map, "resize");
                map.setCenter( bounds.getCenter() );
                map.setZoom( 16 );
            });
        })(jQuery);

谁能建议我做错了什么?非常感谢任何帮助

【问题讨论】:

  • 您是否有示例 HTML 标记以使用 javascript?还是演示问题的小提琴?
  • 有多少个标记?
  • 你可以在这里看到一个例子,在“提到的位置”标签pastonpaper.com/product/…
  • 你有一个css issue also
  • 感谢 CSS 提示 - 我现在已经更新了

标签: javascript jquery wordpress google-maps-api-3


【解决方案1】:

您需要地图被渲染后进行 fitBounds 操作(在它显示并具有最终大小之后)。目前它是在地图隐藏时完成的,因此它适合零大小的地图。请注意,如果您单击“提及的位置”选项卡两次,它会呈现图块,但中心仍位于左上角。

proof of concept example(在显示 div 后 Show/Hide 会 fitBounds,Show 1/Hide 1 使用您的代码)

【讨论】:

  • 谢谢,在按照您的建议渲染地图后,我现在已将代码更新为 fitBounds,但我仍然遇到同样的问题。
  • 我没有看到它在 live version 中发生了变化。该版本仅在 center_map 函数中执行 fitBounds(除非我遗漏了某些内容或存在缓存问题),它仅在页面加载时运行,而不是在显示选项卡时运行。
  • 我已经检查过了,实时版本似乎已经更新:我改变了它,所以 center_map 函数在 render_map 函数之后运行,尽管我的 javascript / jQuery 不是很好,因为你可能会这么说这可能行不通。
  • 问题是 render_map 函数(以及 center_map 函数)在页面加载时运行,而不是在显示选项卡时运行。当它们运行(隐藏选项卡)时,浏览器向 google maps API 报告该 div 的大小为零,并且地图显示您看到的行为(中心位于左上角)。请参阅我添加到答案中的概念证明。
【解决方案2】:

首先,转到放置标签模板的文件,搜索加载地图标签的<li>标签并将其放入:

<li onclick="reloadmap()">

在地图脚本之后

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

这样写:

function reloadmap(){
  //when you resize the map, you lose your zoom and your center
  //so you need to get them again here
    z = map.getZoom();
    c = map.getCenter();
    google.maps.event.trigger(map, 'resize');
  //and set them again here
    map.setZoom(z);
    map.setCenter(c);
}

【讨论】:

  • 请不要在多个问题上逐字发布相同的答案。要么将问题标记为重复问题,要么专门针对每个问题定制答案。
【解决方案3】:

我遇到了同样的问题,在将 dom 元素更改为可见之后,resize 之后地图不可见。原因是隐藏元素的宽度为零。因此地图被初始化为一个零大小的地图。

在我的情况下,在调用 resize 之前更改地图的大小解决了这个问题。

注意:要在初始化后稍后检索地图,您可以将其保存在 jquery 数据对象中,如下所示:

var $map = $('#id-of-the-map');  // get the jquery dom element of the map
var mapInstance = new google.maps.Map($map[0], {... some google maps options ...});
$map.data('mapInstance', mapInstance);

然后在调整大小之前更改 dom 元素的尺寸:

var $map = $('#id-of-the-map');  // get the jquery dom element of the map
if ($map.is(':visible')) {   // if it became visible
    $map.height($map.width() / 1.6);  // resize the dom element
    var mapInstance = $map.data('mapInstance'); // get the google maps map instance
    google.maps.event.trigger(mapInstance, "resize");   // resize the map
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2011-11-05
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2013-08-28
    相关资源
    最近更新 更多