简体   繁体   English

Google Maps V3 - 覆盖 API KEY

[英]Google Maps V3 - Override API KEY

I'm loading Google Maps using the default script tag like the following我正在使用如下所示的默认脚本标签加载 Google 地图

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

Is there a way to override this API_KEY after the page is loaded ?有没有办法在页面加载后覆盖这个 API_KEY? Or even create another instance of google maps without affecting the original instance?或者甚至在不影响原始实例的情况下创建另一个谷歌地图实例?

This is needed for AB testing to avoid it from affecting the original API_KEY that is using throughout the website.这是 AB 测试所必需的,以避免它影响整个网站使用的原始 API_KEY。

Iframe approach is probably not possible because it is needed to append several markers in the map. iframe 方法可能是不可能的,因为需要在地图中附加几个标记。

Thanks.谢谢。

If someone else has the same problem, first it is needed to reset the current instance of maps如果其他人有同样的问题,首先需要重置地图的当前实例

google.maps = undefined;

Then you can just include your own script然后你可以只包含你自己的脚本

var scriptNode = document.createElement('script');
scriptNode.src = GOOGLE_MAPS_URL_WITH_NEW_KEY;
document.head.append(scriptNode);

It works this way.它是这样工作的。

Note: If you have any other dependencies such as Infobox, you might need to add those again注意:如果您有任何其他依赖项,例如 Infobox,您可能需要再次添加这些依赖项

您可以像这样(jQuery)添加编辑您的 src 属性:

$("script").attr("src", YOUR_URL_HERE_WITH_AN_API_KEY_CHANGE);

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

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