【问题标题】:Google Maps does not load in a Bootstrap modal谷歌地图不加载引导模式
【发布时间】:2018-05-18 23:29:56
【问题描述】:

我是这个平台的新手。我是法国人,我用谷歌翻译来翻译这段文字。

我在引导模式(引导程序版本 3.3.7)中初始化 Google 地图时遇到问题

我已经讨论过主题和论坛,但没有任何解决方案。

当您打开模式(单击按钮)时,地图会显示在灰色框中,但不会加载。

当我更改浏览器的大小时,它会加载但不居中。

如何直接加载地图?

地图正在加载 ACF 插件,这是我的代码:

<style type="text/css">

.acf-map {
  width: 100%;
  height: 400px;
  border: #ccc solid 1px;
  margin: 20px 0;
}

/* fixes potential theme css conflict */
.acf-map img {
   max-width: inherit !important;
}

</style>


<script src="https://maps.googleapis.com/maps/api/js?key=#####"></script>
<script type="text/javascript">

(function($) {

// popup is shown and map is not visible

/*
*  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    : 16,
    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 );


  // return
  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( 16 );
  }
  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(){

    // create map
    map = new_map( $(this) );

    // popup is shown and map is not visible
    google.maps.event.trigger(map, 'resize');

  });



});

})(jQuery);

</script>

以及模态引导的整合:

<!-- MAPS -->

<div class="col-md-6 txtmap">


    <div class="animated fadeInRightShort" data-id='4'>
        <button type="button" class="button button-icon btnprogramme big" data-toggle="modal" data-target="#gmaps">
          <i class="fa fa-map-marker"></i>Google Maps</a>
        </button>
    </div>

</div>




<!-- MODAL MAPS -->

<div class="modal fade" id="gmaps" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><?php the_title(); ?> <span>- Google Maps</span></h4>
      </div>
      <div class="modal-body">

            <?php 

            $location = get_field('programme-maps');

            if( !empty($location) ):
            ?>
            <div class="acf-map">
            <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
            </div>
            <?php endif; ?>

      </div>
      <div class="modal-footer">
        <button type="button" class="button alt" data-dismiss="modal">Fermer</button>
      </div>
    </div>
  </div>
</div>

感谢您的帮助! 我不知道了,也不看..

【问题讨论】:

  • 模式是否与查询所在的页面位于同一页面?
  • 感谢您的回答。是的,模态与 jquery 位于同一个文件中。 jquery 在我的 php 文件的末尾。不知道问题出在哪里...

标签: php google-maps bootstrap-modal advanced-custom-fields


【解决方案1】:

感谢您的回答。 我查看了 codepen,我试图根据我的情况对其进行调整,但我没有成功。结果是一样的。

这是结果的截图:

My problem

如果我修改浏览器的窗口,地图就会出现(但不是居中)。

当我查看 ACF 文档时,他告诉我插入此代码。但是不知道在哪里……

解决隐藏地图问题

如果在 隐藏元素。显示元素时,地图将不显示。 使用弹出模式时最有可能出现这种情况。

要解决这个问题,只需触发地图上的“resize”事件 地图元素可见后的变量。

// popup is shown and map is not visible
google.maps.event.trigger(map, 'resize');

非常感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多