Robin's Blog

How to: load the Google Maps Places library through Google API Loader

Google have recently introduced a new way of loading their javascript APIs: their Google API Loader. To use it, all you do is add a script tag in your HTML:

<script src="https://www.google.com/jsapi"></script>

You can then load whatever Google APIs you want using code like this:

google.load('visualization', '1.0');
google.load('jquery', '1');
google.load('maps', '3');

google.setOnLoadCallback(function() {
    console.log('Callback')
});

and the callback function will run after all of the APIs have loaded.

This is far nicer than including individual URLs to APIs as separate script tags, but the documentation is a bit limited. For example, the list of supported APIs doesn’t include the Google Maps API, but sample code from another team within Google (the Earth Engine team) already uses this method of loading the Maps API.

The problem is that I couldn’t find a way to specify that I wanted to load the Google Maps Places library – so I had to go back to including a script tag:

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

But this seemed to conflict with the Google API Loader way of doing things.

Anyway, to cut a long story short, I’ve worked out how to use the Google API Loader to load the API with the Places library. Just do this:

google.load('maps', '3', {other_params:'libraries=places'});

This also works for any other parameters you want to put in the API URL, and may work for other Google APIs (although I haven’t tried it).


If you found this post useful, please consider buying me a coffee.
This post originally appeared on Robin's Blog.


Categorised as: Computing, How To, Programming


3 Comments

  1. Great! Thanks for sharing.

  2. Andrew F. says:

    I was lost until I found your article…
    Thanks for sharing your work. I really apreciate it. Thanks mate!

  3. Duncan says:

    If you look in the JS at https://www.google.com/jsapi where it calls google.loader.rm({…}) you can see the list of supported APIs. I make it about 21 available!

Leave a Reply

Your email address will not be published. Required fields are marked *