I'm trying to make a longitude and latitude picker using mapbox.js and I need this map to be in a modal box which opens when I click on a "choose location" button. But when this modal opens the map is not rendered completely.
this is my code:
<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">×</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>
Script
    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;
    }
and the button action
$('#chooseLocation-btn').click(function(){
   $('#chooseLocation').modal('show');
});  
                I'm not sure whether your issue already fixed or not. However, I also faced the same problem, and I fixed it by using the following code. Posting answer as it may help someone else.
$('#chooseLocation').on('shown.bs.modal', function () { // chooseLocation is the id of the modal.
    map.invalidateSize();
 });
                        here is a good example for a tab using mapbox gl js and map.resize()
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  var target = $(e.target).attr("href") // activated tab
  alert(target);
  map.resize();
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With