Prevent Google maps JS executing multiple times caused by Rails Turbolinks

前端 未结 5 1294
盖世英雄少女心
盖世英雄少女心 2021-02-19 07:30

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 ca

5条回答
  •  我在风中等你
    2021-02-19 08:15

    Instead of make it a regular 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.

提交回复
热议问题