我正在使用包含 initMap 回调函数的 google map API,我需要在 google map API URL 之外调用此函数,例如我正在使用:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"></script>
我需要删除查询字符串的最后一部分 &callback=initMap
但是当我删除 google map 时不起作用。
问题是我想对所有 google api 相关的东西使用这个相同的 google map api,因此一些 API 没有 initMap 方法,这就是为什么我想单独调用这个方法是可能的。
最佳答案
您不必异步调用 initMap
函数。
更改(看起来它缺少“异步延迟”):
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"></script>
收件人:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
然后在 map div 渲染到 DOM 后调用 initMap 函数。
注意:Google 将所有示例更改为异步加载 API(页面加载速度)是有原因的,他们的所有示例最初都是使用 onload 函数完成的。
代码片段:
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>
关于javascript - 有没有办法在 google API URL 之外调用 google initMap 回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40177571/