问题描述
HTML:我需要在引导程序3模式框中加载谷歌地图.js api,然后点击链接并打开modalbox。 / p>
< input id =latclass =text-input form-controltype =textsize =24 name =lat>
< input id =lonclass =text-input form-controltype =textsize =24name =lon>
正常工作,但点击保存更改并返回到模态箱位置选择器后工作后拖动长和不变!
如何解决这个问题?!
DEMO
然后,您可以使用此代码执行所需操作:
var googleMapsLoaded = false;
函数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);
});
});
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
解决方案 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);
});
});
这篇关于位置选择器不起作用在谷歌地图初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!