有没有办法在google API URL之外调用google initMap回调函数?

Kam*_*tti 3 javascript api google-maps

我正在使用包含 initMap 回调函数的谷歌地图 API,我需要在谷歌地图 API URL 之外调用这个函数,例如我正在使用:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"></script>
Run Code Online (Sandbox Code Playgroud)

我需要删除查询字符串的最后一部分,&callback=initMap但是当我删除谷歌地图时不起作用。

问题是我想对所有与 google api 相关的东西使用相同的 google map api,因此某些 API 没有 initMap 方法,这就是为什么我想单独调用这个方法是可能的。

geo*_*zip 5

您不必initMap异步调用该函数。

更改(看起来它缺少“异步延迟”):

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"></script>
Run Code Online (Sandbox Code Playgroud)

到:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
Run Code Online (Sandbox Code Playgroud)

然后在 DOM 中渲染地图 div 后调用 initMap 函数。

注意: Google 将所有示例更改为异步加载 API(页面加载速度)是有原因的,所有示例最初都是使用 onload 函数完成的。

代码片段:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"></script>
Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)