Prevent Google maps JS executing multiple times caused by Rails Turbolinks

半城伤御伤魂 提交于 2019-12-05 12:28:51

问题


I'm currently working on Rails app that is getting the following error:

You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

After a little research I discovered that Turbolinks was causing the problem. When the link_to is clicked all of the DOM elements created by Google maps are retained within the DOM. When a new page is rendered another set of Google Map DOM elements is added causing duplicates and the error.

I could fix this very quickly by simply adding 'data-no-turbolink' => true to my link_to but this defeats the purpose of using Turbolinks as it forces a refresh.

I'm wondering if there's a potential workaround to stop this duplication without turning Turbolinks off?

map.js:

var initMap = function initMap() {

  if (typeof mapLatLng != "undefined") {

    // we can use this to set the map zoom
    // in different places. 
    // use: window.setZoom = 12;
    if (typeof setZoom ==! "undefined") {
      var mapZoom = setZoom;
    } else {
      var mapZoom = 14;
    }

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: mapZoom,
      center: mapLatLng,
      disableDefaultUI: true,
      scrollwheel: false
    });

    var markerSVG = {
      path: 'M1152 640q0-106-75-181t-181-75-181 75-75 181 75 181 181 75 181-75 75-181zm256 0q0 109-33 179l-364 774q-16 33-47.5 52t-67.5 19-67.5-19-46.5-52l-365-774q-33-70-33-179 0-212 150-362t362-150 362 150 150 362z',
      fillColor: '#f32e74',
      fillOpacity: 1,
      strokeWeight: 0,
      anchor: new google.maps.Point(870,1650),
      scale: (0.02, 0.02)
    };

    var mapMarker = new google.maps.Marker({
      position: map.getCenter(),
      map: map,
      icon: markerSVG,
    });
  }
}

view:

<% if @job.address.latitude && @job.address.longitude %>
  <%= javascript_tag do %>
    window.mapLatLng = {lat: <%= @job.address.latitude %>, lng: <%= @job.address.longitude %>};
  <% end %>
  <% content_for :js do %>
    <script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script>
  <% end %>
<% end %>

回答1:


Instead of <script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script> make it a regular <script></script> tag and add the following below your initMap function:

if(window.google){
  initMap();
} else{
  $.ajax('https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap', {
    crossDomain: true,
    dataType: 'script'
  });
}

If you aren't using jquery then just use XMLHttpRequest instead.




回答2:


Adding data-turbolinks-eval="false" to the script tag will tell turbolinks not to re-evaluate it.

<script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap" data-turbolinks-eval="false"></script>

See https://github.com/turbolinks/turbolinks#working-with-script-elements

Worked in our case, where all our script tags were in <head> as per turbolinks docs.




回答3:


Try this too....

google.maps.event.addDomListener(window, 'turbolinks:load', initialize);

And remeber, all google script have to stay after Turbolink, like this:

= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'https://maps.googleapis.com/maps/api/js', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'gmap', 'data-turbolinks-track': 'reload'

See more at: https://github.com/turbolinks/turbolinks




回答4:


I had faced the same problem, unfortunately this might be of no help to you after so many months.

I moved:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=places"></script>

in head tag after <%= csrf_meta_tags %>, and moved the:

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

after /body tag.

By doing so I resolved this issue:

You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

Added data-no-turbolink to the link in navigation bar, the page where I have map. This helped me keep using turbolinks in all pages without that error and as well as let me generate the map when that page was accessed.




回答5:


adding data-turbolinks-eval=false to the script tag solves the problem

See the docks of the turbolinks gem for more details https://github.com/turbolinks/turbolinks-classic#evaluating-script-tags



来源:https://stackoverflow.com/questions/33115375/prevent-google-maps-js-executing-multiple-times-caused-by-rails-turbolinks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!