Error injecting source script from googlemaps via method

2

I would like geocomplete not to get this error, but to work, it depends on the API google maps. I removed the following script from the header and passed via method:

 <script id="id_script" src="https://maps.googleapis.com/maps/api/js?client=nome-cliente&v=3.21&language=pt-br&libraries=places&components=country:Brasil"></script>//(1)códigoremovido

Whereitwasanditworkedlikethis:

<head><scriptsrc="http://code.jquery.com/jquery-1.10.1.min.js"id="jquery"></script>
 <script src="/js/jquery.geocomplete.min.js" type="text/javascript"></script>
 //1. antes ele estava aqui ----
  <script id="id_script" src="default.js"></script>
</head>

The method I created does the same as the script (1) that was loaded in head:

function googleMapsScriptById(client, id) {
    var url_maps = 'https://maps.googleapis.com/maps/api/js?'+client+'v=3.21&language=pt-br&libraries=places&components=country:Brasil';

    (function () {
        var gmaps = document.createElement('script');
        gmaps.type = 'text/javascript';
        gmaps.async = true;
        gmaps.src = url_maps;
        var ref = document.getElementById(id);
        ref.parentNode.insertBefore(gmaps, ref);
    })();
}

googleMapsScriptById('client=nome-cliente&','id_script');

But now I'm getting the following error message from the geocomplete that did not have this:

  

jquery.geocomplete.min.js: 8 Uncaught ReferenceError: google is not defined

What can be, is there any way to adjust the sync between the two?

My conclusions:

Even though I reverse, the loading order:

<head>
<script id="antes_desse"></script>
  <script src="http://code.jquery.com/jquery-1.10.1.min.js"id="jquery"></script>
 <script src="/js/jquery.geocomplete.min.js" type="text/javascript"></script>
  <script id="id_script" src="default.js"></script>
</head> 

Example:

googleMapsScriptById('client=nome-cliente&','antes_desse');

The error still persists, because I understand that the libraries in the head are preloaded before executing the method: googleMapsScriptById(); , and in this case, as the others are already in head, and this is google, still has not been loaded, its dependencies simply display error.

I wonder if there is any way to do this, how do we do with Promise?

    
asked by anonymous 21.10.2016 / 16:46

1 answer

2

Ivan,

The Google Maps Javascript API is being loaded before the Geocomplete API, where it needs the Google API.

In the role you created to enter the Google Maps API, enter Geocomplete later.

UPDATE : The " async " attribute will not block loading of other scripts, if you remove it, the load may be slower, but the Google API Maps is already loaded.

More information here: Google Maps Javascript API

    
24.10.2016 / 15:01