【问题标题】:To access Google maps api, when do I need to use the below scripts?要访问 Google 地图 api,我什么时候需要使用以下脚本?
【发布时间】:2016-06-06 21:12:37
【问题描述】:
<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>

<script async defer  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">   </script>

n 上面的脚本,不同的是API key。我注意到使用和不使用 API KEY 的代码示例。它说 Google Maps Javascript API 2 需要 API 密钥,而 Google Maps Javascript API 3 不需要密钥。但是在脚本中我们没有提到它应该访问哪个版本的 API。那么有什么区别呢?

【问题讨论】:

  • 这两个脚本都包含Google Maps Javascript API v3(v2 已被弃用并且很久以前就关闭了,替换为 v3 的包装器,它不应该用于新代码)。跨度>
  • 您的意思是第一个脚本不应该用于新代码(例如 Google 代码示例)?
  • 第一个应该没问题。如果你使用第二个,你应该可能去掉async defer&amp;callback=initMap
  • 发布的两个脚本都加载了 v3,并且适用于新代码。

标签: javascript google-maps google-maps-api-3 geolocation maps


【解决方案1】:

此脚本在没有 API 密钥的情况下加载 Google Maps Javascript API 的 experimental version

<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>

此脚本使用 API 密钥异步加载 Google Maps Javascript API 的实验版本:

<script async defer  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">   </script>
  • API 密钥允许跟踪使用情况并允许您在需要时购买额外的配额。

对于async defercallback参数,来自这个问题的答案:Page speed - any problems with simply using defer attribute?

async:您可以通过在现代浏览器上使用 async 属性来防止脚本在加载期间阻塞

defer:defer 属性表示在页面 DOM 完全加载之前根本不加载。 Defer 意味着异步。

使用 Google Maps API,您必须在异步加载 API 时使用回调参数。这会导致 api 在内部使用动态脚本插入而不是 document.write 调用。您也可以指定一个空的回调参数。

【讨论】:

    猜你喜欢
    • 2014-10-13
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2010-09-21
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多