【问题标题】:Google map disappear when modal class is active模态类处于活动状态时谷歌地图消失
【发布时间】:2016-10-19 16:06:27
【问题描述】:

我有这个模态:

<form id="contactModal">
<div id="mymodal2" class="" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
     <div class="modal-dialog">
           <div class="modal-content">
              <div class="modal-header">                  
              <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <span class="modal-title th2" id="lblModalLabel" name="lblModalLabel">Contact</span>
            </div>
            <div class="modal-body">

如果我不添加class="modal" 喜欢

<div id="mymodal2" class="fade modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">

我对地图没有任何问题,问题 是当我尝试添加模态类时,我的地图消失了,为什么会这样?

---更新---

如果我运行没有模态类的应用程序,例如:

<div id="mymodal" class="" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" >

然后打开 google chrome 检查器并通过它添加类。它将地图加载到模态中。

这是我的 JS 代码:

 try {
            map = new GMaps({
                div: '#gmap_marker',
                lat: 19.4368277,
                lng: -99.1905598
            });


            map.addMarker({
                lat: 19.4368277,
                lng: -99.1905598,
                title: New City",
                infoWindow: {
                    content: '<strong>City</strong>'
                }
            });
            console.log("gmap processed");
        }
        catch (e) {
            alert(e);
        }

    $("#btnFirstMap").click(function () {


        try {
            map = new GMaps({
                div: '#gmap_marker',
                lat: 19.4368277,
                lng: -99.1905598
            });


            map.addMarker({
                lat: 19.4368277,
                lng: -99.1905598,
                title: "FirstMap",
                infoWindow: {
                    content: '<strong>City2</strong>'
                }
            });
        }
        catch (e) {
            alert(e);
        }

    });

    $("#btnCity3").click(function () {


        try {
            map = new GMaps({
                div: '#gmap_marker',
                lat: 18.6490907,
                lng: -91.8218911
            });


            map.addMarker({
                lat: 18.6490907,
                lng: -91.8218911,
                title: "City3,
                infoWindow: {
                    content: '<strong>City3</strong>'
                }
            });
        }
        catch (e) {
            alert(e);
        }

    });

【问题讨论】:

  • 模态框的默认状态是隐藏的(我相信)。如果您的类为modal,您仍然需要在页面加载后显示模式。 $('#myModal').modal('show');
  • 我添加 $('#contactModal').modal('show');在验证之前,它不起作用
  • contactModal 似乎是 form 根据您的原始代码。如果地图实际上在mymodalmymodal2 内,并且它们的类设置为modal,则这些是需要调用.modal('show')` 的div。
  • 是的,我明白了,但是mymodal 只在加载页面上运行,但我需要在页面加载时加载脚本
  • 您可以将您拥有的任何 JavaScript 包装到函数中,然后按照您的意愿调用它们。有window.onload,或者body 标签也接受onload=someFunction()。页面加载时运行 JavaScript 的方法有很多种。

标签: javascript c# twitter-bootstrap google-maps


【解决方案1】:

确保:

1) 显式设置地图容器大小,例如通过 CSS:

div#map {
    width: 560px !important;
    height: 600px !important;
}

2) 通过resize 事件显示模态对话框后重新加载地图:

$('#myModal').on('show.bs.modal', function(e) {
    var map = createMap();
    google.maps.event.addListenerOnce(map, 'idle', function() {
        google.maps.event.trigger(map, 'resize'); 
    });
}) 

示例

$(document).ready(function() {
   
    $('#myModal').on('show.bs.modal', function(e) {
        var map = createMap();
        google.maps.event.addListenerOnce(map, 'idle', function() {
            google.maps.event.trigger(map, 'resize'); 
        });
    })

});


function createMap() {
    var mapControl = new GMaps({
        div: '#map',
        lat: -12.043333,
        lng: -77.028333
    });
    mapControl.addMarker({
        lat: -12.043333,
        lng: -77.03,
        title: 'Lima',
        details: {
            database_id: 42,
            author: 'HPNeo'
        },
        click: function(e) {
            if (console.log)
                console.log(e);
            alert('You clicked in this marker');
        }
    });
    return mapControl.map;
}
div#map {
    width: 560px !important;
    height: 600px !important;
}
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.2.js"></script>
<script type="text/javascript" src="http://getbootstrap.com/dist/js/bootstrap.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="https://hpneo.github.io/gmaps/gmaps.js"></script>


<link rel="stylesheet" type="text/css" href="http://getbootstrap.com/dist/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">


 
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" id="modalOne" data-toggle="modal" data-target="#myModal">Show map</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>

                </button>
                <h4 class="modal-title" id="myModalLabel">Map</h4>

            </div>
            <div class="modal-body">
                <div id="map"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

Codepen

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多