简体   繁体   中英

google map in bootstrap modal not working properly

I'm trying to show a google map in bootstrap modal for some days now, i searched all the answers here discussing this topic but till now nothing worked for me. I understand that I have to use the resize trigger after the modal is open and indeed i used it but nothing seems to work for me and it is very frustrating. PLS help!

here is the code i used:

<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
myLatlng = new google.maps.LatLng(32.0260053,34.8987034);
function initialize() {
    var myOptions = {
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
                              myOptions);        
    $('.myMapModal').on('show.bs.modal', function() {

        google.maps.event.trigger(map, 'resize');
        return map.setCenter(myLatlng);
    });
}
google.maps.event.addDomListener(window, 'load', initialize);

the HTML:

<div class="modal myMapModal fade" id="myMapModal" >
        <div class="modal-dialog">
            <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">#f16#</h4>
                </div>
                <div class="modal-body map-modal">
                    <div class="container">
                        <div class="row">
                            <div id="map-canvas" style="width: 500px; height: 480px;"></div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">סגירה</button>
                </div>
            </div>
        </div>
    </div>  

I got it with the following, it may help others.

BootstrapDialog.show({
            title: 'Point on Map',
            message: ''<div id="selction_map"></div>'',
            onshown: function(dialogRef){
                initMap();
            }
        });

https://nakupanda.github.io/bootstrap3-dialog/

Check out this: JSFiddle can serve you

$("#myMapModal").ready(function() {
    $(window).resize(function() {
        google.maps.event.trigger(map, 'resize');
    });
    $('#myMapModal').modal('show');
    google.maps.event.trigger(map, 'resize');
    return map.setCenter(myLatlng);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM