apneadiving / Google-Maps-for-Rails

Enables easy Google map + overlays creation in Ruby apps
https://apneadiving.github.io/
MIT License
2.27k stars 382 forks source link

Issue with Bootstrap 3 tabs #454

Closed conexionagenciadigital closed 10 years ago

conexionagenciadigital commented 10 years ago

I'm using gmaps4rails and Bootstrap on a rails app to add a map in a model's show view. The map goes inside a tab using Bootstrap JS tabs. However, the map doesn't gets displayed correctly, it just shows a corner and the center is outside the map div. I've tried to do google.maps.event.trigger(map, 'resize'); according to Stackoverflow, but haven't being able to fix it yet. Any thoughts?

screen shot 2014-09-11 at 3 05 08 pm

apneadiving commented 10 years ago

You should create the map once tab is activated. Google is looking for the size of the element, so if its not visible the result is not aceptable

conexionagenciadigital commented 10 years ago

Awesome!

I finally moved your code to a createMap() function which gets called once the tab is shown:

// Google maps 4 rails function createMap(){ handler = Gmaps.build('Google'); handler.buildMap({ provider: {}, internal: {id: 'map'}},

        function(){
            markers = handler.addMarkers(<%=raw @hash.to_json %>);
            handler.bounds.extendWith(markers);
            handler.fitMapToBounds();
            handler.getMap().setZoom(13);
        }); 
};

$("#myTab a[href='#location'").on('shown.bs.tab',function(){
    createMap();
});

Thanks a lot, super fast response!

screen shot 2014-09-11 at 3 34 21 pm