javascript - Google Maps basic embed doesn't work on mobiles
问题描述
I'm making simple website and I want google map there. I need dynamic map with one localization mark. I didn't want to use google API, because I read that key is limited use. So I used google maps "share" option. It works fine on desktop everytime, but it only works sometimes on mobiles. Does somebody know why? Here's the code:
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d1240.111699850084!2d21.838264!3d51.564138!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47189c715ab7a54b%3A0x9563a208da50b7d3!2sKrzywa%204A%2C%2008-530%20D%C4%99blin!5e0!3m2!1spl!2spl!4v1614375043434!5m2!1spl!2spl" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
mobile
desktop
解决方案
Sounds like a lame thing to say, but it works on my machine.
Check this on your mobile, made reproduction: https://jsfiddle.net/soqj3f6z/
<iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d1240.111699850084!2d21.838264!3d51.564138!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47189c715ab7a54b%3A0x9563a208da50b7d3!2sKrzywa%204A%2C%2008-530%20D%C4%99blin!5e0!3m2!1spl!2spl!4v1614375043434!5m2!1spl!2spl" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
It looks like you served your website locally on a desktop and then it works but when you open it on your mobile reaching your desktop via local IP it is not considered safe anymore and thus the script from other domain gets blocked. (Read about what CORS /CSP is).
To make some tests confirming my guess: try to open this url from another desktop and check the console for errors. Or try to deploy it to any server with SSL (https) so the Google maps still thinks it's embedded in a safe context.
Jsfiddle has SSL and thus google maps allows being embedded there.
推荐阅读
- javascript - 更改按钮的方向
- .net - 如何从 .NET (C#) 程序(在 Linux 下)调用本机(C 或汇编)代码?
- c++ - 为什么访问未初始化数组中的元素会输出奇怪的值?
- php - 致命错误:composer.lock 是为 PHP 7.4 或更高版本创建的,但当前 PHP 版本是 7.1.33
- javascript - 如何更改高图表中图表区域的背景颜色?
- c# - 提供枚举时,C# Web API 中的 POST 方法接收 Null 模型
- python-3.x - 用于文本分析的英语词典转储
- microsoft-graph-api - 尝试对 Microsoft 团队通话进行分析
- javascript - 如何在 Pos odoo 中加载 qty_avaiable
- typescript - 属性“zeroValue”没有初始值设定项,也没有在构造函数中明确分配