Google Maps Key

Asked

Viewed 130 times

0

I have a function that carries the API of Google Maps, but every time I try to change the url, whether to use my key, or even to add to library Places to be able to use the search services I have problems.

The function I’m using is this:

function loadScript() {
     var script = document.createElement('script');
     script.type = 'text/javascript';
     script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'callback=initialize';
     document.body.appendChild(script);
 };

2 answers

0

And it turns out I’ve solved the problem, here’s the solution in case someone has something like this:

script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'libraries=places&' + 'callback=initialize';
  • You do not need to concatenate the string in this case. Since it has no dynamic value, just take the string directly. script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&callback=initialize';

0

In fact, creating a command to load a script is a bit complicated and can cost processing. In my scripts, I always call the Google Maps API, followed by the commands async and defer. These commands will ensure that the API is called in the background, that is, while you run your HTML, the API will be downloaded "together". An example of a call with async defer that’s the one:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=init&libraries=places" async defer></script>

  • Explaining with examples helps even more the answer. As it is an answer to a very old question, maybe it is also cool to update the question, if you have changed the function call, code, etc.

  • That’s right, it’s been over a year since this question was asked...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.