iframe内的Google地图无法加载

Ben*_*enM 24 javascript jquery google-maps

我遇到了一个奇怪的问题,我不知道问题是什么.以下jQuery代码是我想要实现的简化版本:

var iframe = $('<iframe />');
iframe.prop('src', 'https://maps.google.com/maps?q=London&hl=en&sll=37.0625,-95.677068&sspn=46.677964,93.076172&t=h&hnear=London,+United+Kingdom&z=10');
iframe.appendTo($('body')); 

// When the iframe loads:
iframe.load(function() {
    alert(1);
});
Run Code Online (Sandbox Code Playgroud)

永远不会加载地图,并且load()永远不会触发事件.Chrome报告以下错误:

Refused to display 'https://maps.google.com/maps?q=London&hl=en&sll=37.0625,-95.677068&sspn=46.677964,93.076172&t=h&hnear=London,+United+Kingdom&z=10' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

怎么绕过这个?

这是一个jsFiddle演示.

Ben*_*enM 43

附加&output=embed到URL的末尾可以解决问题.

  • @pomarc不保证downvote.当答案在一年多前发布时,这是有效的.请考虑撤销你的苛刻的downvote ... (10认同)
  • 我可以确认,2020 年 11 月,output=embed 不再起作用。 (3认同)

Rap*_*tor 16

截至2014年,该选项&output=embed不再适用.Google建议您切换到Google Maps Embed API.这是一个快速入门.

基本上,新的iframe链接是:

https://www.google.com/maps/embed/v1/place?key={BROWSER_KEY}&q={YOUR_ADDRESS_ENCODED}
Run Code Online (Sandbox Code Playgroud)

务必在API控制台中启用Google Maps Embed API.

ps在我写这个答案时检查了工作

  • @MarceloAgimóvel 这是一项完全免费的地图服务,但需要提供电子邮件地址。看起来价格合理 (2认同)

red*_*hka 11

除了places API之外,请务必启用google maps embed api.

从这里生成您的地图:

https://developers.google.com/maps/documentation/embed/start