如何仅在我的应用 navigator.onLine 时加载谷歌地图 API

how to load the google maps API only when my app is navigator.onLine?

本文关键字:加载 谷歌地图 API onLine navigator 何仅 我的 应用      更新时间:2023-09-26

如何添加一些只加载谷歌地图API的javascript:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

当我的应用程序联机时(navigator.onLine == true)?

使用动态脚本插入:

if(navigator.onLine) {
  var script = document.createElement('script');
  //You must use the callback parameter when loading the script asynchronously
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=initialize';
  var scripts = document.getElementsByTagName('script');
  scripts[0].parentNode.insertBefore(script, scripts[0]);
}
function initialize() {
  //initialize map here
}

请参阅 Google 地图 API v3 文档中的异步加载主题。

您可能还对 Google 地图 API v3:针对移动设备进行开发感兴趣