How to check if Google Maps API is loaded?

前端 未结 10 2207
花落未央
花落未央 2020-12-04 16:12

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 h

10条回答
  •  猫巷女王i
    2020-12-04 17:10

    Just so you know, there's an issue with the accepted answer. It will return true if the script has loaded otherwise 'typeof google' may return undefined and throw an error. The solution to this is:

    if ('google' in window && typeof google === 'object' && typeof google.maps === 'object') {...}

    This makes sure a boolean value is always returned.

提交回复
热议问题