0

如何添加一些仅加载 google maps api 的 javascript:

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

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

4

1 回答 1

1

使用动态脚本插入:

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 Maps API v3 文档中的异步加载主题

您可能还对Google Maps API v3 感兴趣:为移动设备开发

于 2013-10-03T12:48:40.910 回答