How to check if Google Maps API is loaded?

前端 未结 10 2222
花落未央
花落未央 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条回答
  •  没有蜡笔的小新
    2020-12-04 17:09

    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.

提交回复
热议问题