如何检查Google Maps API是否已加载?

问题描述:

如何检查Google Maps API(v3)是否已加载?

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

如果由于互联网连接问题导致API未加载,我不想执行映射脚本(网页托管在本地)。

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

if(google.maps){...} 会给你一个参考错误,如果谷歌是未定义的(即如果API没有加载)。

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

相反,使用 if(typeof google ==='object'&& typeof google.maps ==='object'){...} 来检查它是否成功加载。

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