【问题标题】:Multiple Google Maps & Bootstrap tabs only rendering on window resize多个 Google 地图和引导选项卡仅在窗口调整大小时呈现
【发布时间】:2016-05-10 20:31:58
【问题描述】:

您好,我有一些引导选项卡和多个谷歌地图。

我正在使用带有 ACF Google 地图插件的 Wordpress。

切换选项卡时,第一个选项卡将以正确的坐标呈现谷歌地图并按应有的方式显示。我单击的下一个选项卡链接显示一个空白/灰色的 Google 地图,但在调整浏览器窗口的大小后,地图会正确显示。

我尝试了互联网上的一些解决方案,但均无济于事。

这是我的代码

(function($) {

/*
*  new_map
*
*  This function will render a Google Map onto the selected jQuery element
*
*  @type  function
*  @date  8/11/2013
*  @since 4.3.0
*
*  @param $el (jQuery element)
*  @return  n/a
*/


function new_map( $el ) {

// var
var $markers = $el.find('.marker');

// vars
var args = {
    zoom    : 11,
    center    : new google.maps.LatLng(0, 0),
    mapTypeId : google.maps.MapTypeId.ROADMAP
};

// create map           
var map = new google.maps.Map( $el[0], args);

// add a markers reference
map.markers = [];

// add markers
$markers.each(function(){
    add_marker( $(this), map );
});

// center map
center_map( map );
google.maps.event.addListenerOnce(map, 'idle', function() {
google.maps.event.trigger(map, 'resize');
google.maps.visualRefresh = true;  

});
return map;  
}

/*
*  add_marker
*
*  This function will add a marker to the selected Google Map
*
*  @type  function
*  @date  8/11/2013
*  @since 4.3.0
*
*  @param $marker (jQuery element)
*  @param map (Google Map object)
*  @return  n/a
*/

function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'),    $marker.attr('data-lng') );

// create marker
var marker = new google.maps.Marker({
position  : latlng,
map     : map
});

// add to array
map.markers.push( marker );

// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
  content   : $marker.html()
});

// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {
  infowindow.open( map, marker );
});
}
}

/*
*  center_map
*
*  This function will center the map, showing all markers attached to this map
*
*  @type  function
*  @date  8/11/2013
*  @since 4.3.0
*
*  @param map (Google Map object)
*  @return  n/a
*/

function center_map( map ) {

  // vars
  var bounds = new google.maps.LatLngBounds();

  // loop through all markers and create bounds
  $.each( map.markers, function( i, marker ){

  var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );

  bounds.extend( latlng );
});

// only 1 marker?
if( map.markers.length == 1 )
{
// set center of map
  map.setCenter( bounds.getCenter() );
  map.setZoom( 11 );
}
else
{
// fit to bounds
map.fitBounds( bounds );
}

}

/*
*  document ready
*
*  This function will render each map when the document is ready (page has loaded)
*
*  @type  function
*  @date  8/11/2013
*  @since 5.0.0
*
*  @param n/a
*  @return  n/a
*/
// global var

var map = null;
$(document).ready(function(){
  $('.acf-map').each(function(){
       map = new_map( $(this));       
  });
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function(e){      
        google.maps.event.trigger(map, 'resize');  
});

})(jQuery);//end closure main function

【问题讨论】:

    标签: jquery wordpress twitter-bootstrap google-maps advanced-custom-fields


    【解决方案1】:

    这是因为隐藏标签的高度=0,所以地图也被折叠且不可见。

    将此添加到您的 CSS 文件中以解决该问题(这些规则必须在 bootstrap.css 文件之后执行):

    .tab-pane {
        display: block !important;
        position: fixed;
        right: -9999px;
        width: 100%;
    }
    .tab-pane.active {
        position: relative;
        left: 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 2022-01-08
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多