mapbox.js无法在Bootstrap Modal中完全呈现

Arr*_*ron 5 javascript mapbox twitter-bootstrap-3

我正在尝试使用mapbox.js创建一个经度和纬度选择器,我需要这个地图位于一个模式框中,当我点击"选择位置"按钮时会打开它.但是当这个模态打开时,地图不会完全呈现.

这是我的代码:

<div class="modal fade" id="chooseLocation" tabindex="-1" role="dialog" aria-labelledby="chooseLocation" aria-hidden="true">
    <div class="Cadd-event-modal" style="width: 600px; margin: 0px auto; margin-top: 10%;">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title Cfont-1">Choose location</h4>
        </div>
        <div class="modal-body" style="width: 500px; margin: 0px auto;">

            <div id='map'></div>
            <div class='map-overlay'>
                <label for='latitude'>latitude</label><br />
                <input type='text' size='5' id='latitude' /><br />
                <label for='longitude'>longitude</label><br />
                <input type='text' size='5' id='longitude' />
            </div>


        </div>
        <div class="model-footer"></div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

脚本

    var map = L.mapbox.map('map', 'examples.map-9ijuk24y')
           .setView([0, 0], 2);

    // get the form inputs we want to update
    var latitude = document.getElementById('latitude');
    var longitude = document.getElementById('longitude');

    var marker = L.marker([0, 0], {
        draggable: true
    }).addTo(map);

    // every time the marker is dragged, update the form
    marker.on('dragend', ondragend);

    // set the initial values in the form
    ondragend();

    function ondragend() {
        var ll = marker.getLatLng();
        latitude.value = ll.lat;
        longitude.value = ll.lng;
    }
Run Code Online (Sandbox Code Playgroud)

和按钮动作

$('#chooseLocation-btn').click(function(){
   $('#chooseLocation').modal('show');
});  
Run Code Online (Sandbox Code Playgroud)

Rav*_*lya 9

我不确定你的问题是否已经解决.但是,我也遇到了同样的问题,我使用下面的代码修复了它.发布答案,因为它可能会帮助别人.

$('#chooseLocation').on('shown.bs.modal', function () { // chooseLocation is the id of the modal.
    map.invalidateSize();
 });
Run Code Online (Sandbox Code Playgroud)