要访问 Google 地图 api,我什么时候需要使用以下脚本?
To access Google maps api, when do I need to use the below scripts?
<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>
and
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> </script>
n the above scripts, the difference is API key. I have noticed code samples working with and without API KEY. It says Google Maps Javascript API 2 requires API key and Google Maps Javascript API 3 doesn't require the key. But in the scripts we are not mentioning which version of API it should access. So what makes the difference?
此脚本在没有 API 键的情况下加载 Google 地图 Javascript API 的 experimental version:
<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>
此脚本使用 API 键异步加载 Google 地图 Javascript API 的实验版本:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> </script>
- API 密钥允许跟踪使用情况,并允许您在需要时购买额外的配额。
对于async defer
和callback
参数,来自这个问题的回答:Page speed - any problems with simply using defer attribute?
async: You can prevent a script from blocking during load by using the async attribute on modern browsers
defer: The defer attribute indicates not to load at all until the page DOM has completely loaded. Defer implies async.
With the Google Maps API, you must use a callback parameter when loading the API asynchronously. This causes the api to use dynamic script insertion rather than document.write calls internally. You can specify an empty callback parameter as well.
<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>
and
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> </script>
n the above scripts, the difference is API key. I have noticed code samples working with and without API KEY. It says Google Maps Javascript API 2 requires API key and Google Maps Javascript API 3 doesn't require the key. But in the scripts we are not mentioning which version of API it should access. So what makes the difference?
此脚本在没有 API 键的情况下加载 Google 地图 Javascript API 的 experimental version:
<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>
此脚本使用 API 键异步加载 Google 地图 Javascript API 的实验版本:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> </script>
- API 密钥允许跟踪使用情况,并允许您在需要时购买额外的配额。
对于async defer
和callback
参数,来自这个问题的回答:Page speed - any problems with simply using defer attribute?
async: You can prevent a script from blocking during load by using the async attribute on modern browsers
defer: The defer attribute indicates not to load at all until the page DOM has completely loaded. Defer implies async.
With the Google Maps API, you must use a callback parameter when loading the API asynchronously. This causes the api to use dynamic script insertion rather than document.write calls internally. You can specify an empty callback parameter as well.