ntheorist
ntheorist

Reputation: 109

Delaying loading Google Maps API until map is in view

I'm developing a plugin using Google Maps JavaScript API v3, and would like to minimize the number of requests this uses on a heavily trafficked website. Currently the map is fairly far down the page, so I'd like to defer loading the API and incurring request costs unless a user actually scrolls down to it and the map is in view.

I can easily check when the map element is actually in view, my question is, does anyone know if the API call counts as a request when the gapi library script itself is loaded? (the URL with the API key in it is loaded into the page), or does the request only count when a map is actually initialized (ie. map = new google.maps.Map()..)?

thanks!

Upvotes: 0

Views: 1640

Answers (1)

Arturo Lopez
Arturo Lopez

Reputation: 1

You have to remove the callback function that the API gives you.

<script src="https://maps.googleapis.com/maps/api/js?key=TU_API_KEY&callback=initMap()"></script>

So that it looks like this:

<script src="https://maps.googleapis.com/maps/api/js?key=TU_API_KEY"></script>

and then just call the initMap() function at any point in the code

a = 45;
b = 2;
initMap(); // Aqui se manda a ejecutar

Upvotes: 0

Related Questions