位置选择器不起作用在谷歌地图初始化 [英] location picker not work in google map initialize

查看:119
本文介绍了位置选择器不起作用在谷歌地图初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

< div class =modalid =myModal>
< div class =modal-dialog>
< div class =modal-content>
< div class =modal-header>

JS:

  var stillPresent = false; 

函数initialize(){
if(stillPresent == false){
$('#us2')。locationpicker({
location:{
纬度:46.15242437752303,
经度:2.7470703125
},
半径: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');
});



函数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); $($#

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

})

点击在modalbox中保存更改后,我添加两个输入以保存经度和纬度。第一次位置选择器正常工作,但点击保存更改并返回到模态箱位置选择器后工作后拖动长和不变



如何解决这个问题?!



DEMO > loadScript ,每次显示模式时,您都会每次包含Google Maps API。这会在您的网页上产生无法预料的结果。



您需要做的是将Google Maps API包括一次(使用标志避免多次加载),然后调整大小显示Bootstrap Modal时的Google Map。



使用这个问题的答案:
显示谷歌地图的模式创建与Twitter引导



然后,您可以使用此代码执行所需操作:

  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); 
    });
});

Fiddle here

这篇关于位置选择器不起作用在谷歌地图初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆