2

I need to load google map .js api in bootstrap 3 modal box after click in link and open modalbox using google map initialize method like this :

HTML:

<input id="lat" class="text-input form-control" type="text" size="24" name="lat">
<input id="lon" class="text-input form-control" type="text" size="24" name="lon">
<a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a>

<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                 <h4 class="modal-title">Modal title</h4>

            </div>
            <div class="modal-body">Location:
                <input type="text" id="us2-address" style="width: 200px" />Radius:
                <input type="text" id="us2-radius" />
                <div id="us2" style="height: 400px;"></div>Lat.:
                <input type="text" id="us2-lat" />Long.:
                <input type="text" id="us2-lon" />
            </div>
            <div class="modal-footer"> <a href="#" data-dismiss="modal"   class="btn">Close</a> <a href="#" class="btn btn-primary" id="save-changes">Save changes</a>

            </div>
        </div>
    </div>
</div>

JS:

var stillPresent = false;

function initialize() {
    if (stillPresent == false) {
        $('#us2').locationpicker({
            location: {
                latitude: 46.15242437752303,
                longitude: 2.7470703125
            },
            radius: 300,
            inputBinding: {
                latitudeInput: $('#us2-lat'),
                longitudeInput: $('#us2-lon'),
                radiusInput: $('#us2-radius'),
                locationNameInput: $('#us2-address')
            }
        });
        stillPresent = true;
        $("#save-changes").click(function() {
        $("#lat").val($('#us2-lat').val());
        $("#lon").val($('#us2-lon').val());        
        $('#myModal').modal('hide');
        });
    }
}

function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://maps.google.com/maps/api/js?sensor=false&libraries=places&callback=initialize';
    document.body.appendChild(script);
}

$(function () {
    $('#myModal').on('shown.bs.modal', function (e) {
        loadScript();
    });

})

I add two input for save longitude and latitude after click save changes in modalbox. in first time location picker worked true but after click save changes and back to modalbox location picker not work and after drag long and lat not changed!!

how do fix this ?!

DEMO JSFIDDLE

Pink Code
  • 1,802
  • 7
  • 43
  • 65

1 Answers1

2

Because you are calling loadScript each time the modal is shown, you keep including the Google Maps API each time. This will have unpredictable results on your page.

What you need to do is include the Google Maps API once (using a flag to avoid loading it multiple times) and then resize the Google Map when the Bootstrap Modal is shown.

Using the answers to this question: Showing a Google Map in a modal created with Twitter Bootstrap

You can then do what you want using this code:

var googleMapsLoaded = false;

function loadScript() {
    if (!googleMapsLoaded) {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'http://maps.google.com/maps/api/js?sensor=false&libraries=places&callback=initialize';
        document.body.appendChild(script);
        googleMapsLoaded = true;
    }
}

$(function() {
    $('#myModal').on('shown.bs.modal', function (e) {
        loadScript();
        var map = $('#us2').locationpicker('map').map;
        var currentCenter = map.getCenter();
        google.maps.event.trigger(map, "resize");
        map.setCenter(currentCenter); 
    });
});

Fiddle here

Community
  • 1
  • 1
mccannf
  • 16,619
  • 3
  • 51
  • 63
  • i need to load google map api after click in link. google map is a big `.js` file and this not good for loading page. i need to load google map into modal box after click in link. in your fiddle google map load with page. – Pink Code Oct 13 '14 at 18:34