多个Google地图&仅在窗口上呈现的引导选项卡调整大小

时间:2016-02-02 06:36:20

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

您好我有一些引导标签和多个谷歌地图。

我正在使用带有ACF Google Maps插件的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

1 个答案:

答案 0 :(得分:1)

这是因为隐藏的标签高度为0,因此地图也会折叠并且不可见。

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

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