简体   繁体   English

如何动态加载谷歌地图库

[英]How to load google maps libraries dynamically

I know I can load libraries in google maps api v3 using libraries argument in URL . 我知道我可以使用URL中的libraries参数在google maps api v3中加载库 Now I am in the situation where I cannot change the html markup, only javascript. 现在,我无法更改html标记,只能更改javascript。 Can google maps libraries be loaded dynamically using some javascript call, eg by calling some google api function? 可以使用某些javascript调用(例如通过调用某些google api函数)动态加载google maps库吗? Eg something like google.load() . 例如google.load()类的东西。

I know this question is rather old, but I had the same problem recently. 我知道这个问题比较老,但是最近我也遇到了同样的问题。 In my case I couldn't easily change the script-load-url but attach js-code afterwards. 就我而言,我无法轻易更改script-load-url,但之后再附加js代码。 Double loading google-maps-js couldn't solve the problem as well beecause it is prohibited by google. 双重加载google-maps-js无法解决此问题,因为google禁止这样做。

To use libraries I copied the code blocks I needed from the respective urls. 为了使用库,我从相应的URL复制了所需的代码块。 For loading places it has looked like: 对于加载地点,它看起来像:

goto: https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&language=de&libraries=places 转到: https : //maps.googleapis.com/maps/api/js? key = YOUR_API_KEY & language = de & libraries =places

and copy'paste the last block which looks like: 并复制'粘贴最后一块,如下所示:

google.maps.__gjsload__('places', function(_){ ... })

include it in your script after loading the default api. 加载默认api后,将其包含在脚本中。

Remember: This is rather dirty but could solve problems for some of you. 切记:这很脏,但可以为您中的某些人解决问题。 Cheers! 干杯!

Google maps documentation show a way to asynchronously load the API Google Maps文档显示了一种异步加载API的方法

Code from that page: 该页面中的代码:

function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src="http://maps.googleapis.com/maps/api/jskey=API_KEY&sensor=TRUE_OR_FALSE&callback=initialize";
  document.body.appendChild(script);
}
window.onload = loadScript;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM