如何检查Google Maps API是否已加载? [英] How to check if Google Maps API is loaded?
本文介绍了如何检查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.
这篇关于如何检查Google Maps API是否已加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文