Search code examples
javascriptruby-on-railsgmaps4rails

When revisiting home page, gmaps won't reload


I have an app using the Google Maps API gem

    gem 'gmaps4rails'

I have the map on the home page. The page loads fine when it first loads, then if a go over to another page the content of that page loads, but when I go back to the home page, the previous content remains and the map doesn't load. If you manually refresh the page it loads normally.

I am pretty sure this is a simple error that I am missing, how do I get the page to refresh when I revisit it?

My homepage code with the scripts is here

    <div style='width: 400px;'>
        <div id="map" style='width: 400px; height: 400px;'></div>
    </div>

    <script src="//maps.google.com/maps/api/js?key="" + ENV['GOOGLE_API_KEY']></script>
    <script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
    <script type="text/javascript">
    handler = Gmaps.build('Google');
    handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
      markers = handler.addMarkers([
        {
          "lat": -32.023598,
          "lng": 115.859690,
          "picture": {
            "width":  32,
            "height": 32
          },
          "infowindow": "hello!"
        }
      ]);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
      handler.getMap().setZoom(16);
    });
    </script>

I found a few answers that were kind of similar but I could not get the issue solved.


Solution

  • Problems is the turbolink.

    Disable turbolink on all link_to the page has map or remove require turbolink on you app/assets/javascripts/application.js

    Example:

    <%= link_to('HOME', root_path, 'data-no-turbolink' => true) %>