简体   繁体   English

多个bootstrap折叠div上的地图

[英]maps on multiple bootstrap collapse div

I have two collapsible bootstrap divs. 我有两个可折叠的引导div。 In each div i insert a map from google maps. 在每个div中,我从Google Maps中插入一张地图。 The first works fine, but the second opens with gray background, and the map not loads. 第一个工作正常,但是第二个打开时显示灰色背景,并且不会加载地图。

I resize the map but it's not working. 我调整了地图大小,但无法正常工作。

my HTML 我的HTML

<div class="content">
<div class="more">
 <a data-toggle="collapse" class="as" href="#extra-1">info</a>
</div>
</div>
<div id="extra-1" class="extra-info collapse">
<div class="maps" data-lon="2.159154" data-lat="41.397207"></div>
</div>
<div class="content">
<div class="more">
 <a data-toggle="collapse" class="as" href="#extra-2">info</a>
</div>
</div>
<div id="extra-2" class="extra-info collapse">
<div class="maps" data-lon="2.159154" data-lat="41.397207"></div>
</div>

and JS 和JS

 $( document ).ready(function() {

   window.obj = window.obj || {};
        var obj = window.obj;
obj.mapRendering = {
            eachMap: $('.extra-info'),

            init: function () {
                this.oneMap();
            },

            oneMap: function(){
                var that = this;

                this.eachMap.each(function(a, b){
                    $(b).on('show.bs.collapse', function(){

                        if($(this).attr("loaded") == "true") {
                            console.log("revisited");
                            return this;
                        }

                        var selector = $(this).find('.maps');
                        console.log(selector[0]);

                        var lati = selector.attr("data-lat");
                        var longi = selector.attr("data-lon");

                        var latlng = new google.maps.LatLng(parseFloat(lati),parseFloat(longi));
                        var myOptions = {
                            zoom: 16,
                            center: latlng,
                            mapTypeId: google.maps.MapTypeId.ROADMAP,
                            disableDefaultUI: true,
                            mapTypeControl: false,
                            zoomControl: true
                        };
                        var map = [];
                        map[a] = new google.maps.Map(selector[0], myOptions);

                        google.maps.event.trigger(map[a], 'resize');
                        $(this).attr("loaded", "true");

                        return this;
                    });
                });
            }
        };
 obj.mapRendering.init();
    });

fiddle 小提琴

Thanks, Alex. 谢谢,亚历克斯。

You'd probably need to change the callback function on bootstrap, so google maps is called when the animation has finished. 您可能需要更改引导程序上的回调函数,所以动画结束后将调用google maps。 So instead of: 所以代替:

$(b).on('show.bs.collapse', function(){
  ...
})

You'd have: 您将拥有:

$(b).on('shown.bs.collapse', function(){
  ...
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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