Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Google Maps API is loaded?

How to check if Google Maps API (v3) is loaded?

I do not want to execute mapping scripts if the API did not load due to internet connectivity problems (web page is hosted locally).

like image 306
Nirmal Avatar asked Feb 10 '12 14:02

Nirmal


People also ask

How do I know if my Google API is loaded?

if (google. maps) {...} will give you a reference error if google is undefined (i.e. if the API didn't load). Instead, use if (typeof google === 'object' && typeof google. maps === 'object') {...} to check if it loaded successfully.

Why isn't my google maps API working?

There are a several reasons why your google maps may not be working, the most common issue being no Google Map API key set or set incorrectly. To use the Google Maps JavaScript API, you must register your app project on the Google Cloud Platform Console and get a Google API key which you can add to your app.


1 Answers

if (google.maps) {...} will give you a reference error if google is undefined (i.e. if the API didn't load).

Instead, use if (typeof google === 'object' && typeof google.maps === 'object') {...} to check if it loaded successfully.

like image 86
DaveS Avatar answered Oct 04 '22 04:10

DaveS