Google Maps API 演示 - google 未定义
P粉269847997
P粉269847997 2023-09-02 18:33:34
[HTML讨论组]
<p>我正在尝试实现Google Maps API,并且一直在关注官方的Demo。</p> <p>我复制了代码,只做了一个更改:</p> <pre class="brush:php;toolbar:false;">// Raises an error: Access to script at 'file:///D:/Desktop/Stuff/map-test/test.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted. &lt;script type=&quot;module&quot; src=&quot;./index.js&quot;&gt;&lt;/script&gt; // Changed to this &lt;script src=&quot;./index.js&quot;&gt;&lt;/script&gt;</pre> <p>结果,我收到以下错误:</p> <pre class="brush:php;toolbar:false;">ReferenceError: google is not defined at initMap</pre> <p>我猜这是由于从脚本中删除了 <code>type="module"</code> 造成的?大多数人如何实现它,因为似乎您需要自己托管模块才能为此导入它?</p> <p>我已将代码更改为以下内容,但这会引发警告。</p> <pre class="brush:php;toolbar:false;">&lt;script type=&quot;text/javascript&quot; src=&quot;https://maps.googleapis.com/maps/api/js?key=API_KEY&quot;&gt;&lt;/script&gt; // Warning: Google Maps JavaScript API has been loaded directly without a callback. This is not supported and can lead to race conditions and suboptimal performance. For supported loading patterns please see https://goo.gle/js-api-loading</pre> <p>谢谢!</p> <p>编辑:</p> <p>我阅读了之前关于使用 HTTP 服务器托管文件的答案,我误解了他们的意思是托管 JS 文件。相反,您应该在本地托管 HTML 文件以进行测试,而不是直接打开 HTML 文件(即在文件资源管理器中双击它)。</p> <p>所以使用 python3:</p> <pre class="brush:php;toolbar:false;"># in the directory that contains index.html: python3 -m http.server 8080</pre> <p>访问 http://localhost:8080/index.html 对我有用。谢谢!</p>
P粉269847997
P粉269847997

全部回复(1)
P粉356361722

我阅读了之前关于使用 HTTP 服务器托管文件的答案,我误解了他们的意思是托管 JS 文件。相反,您应该在本地托管 HTML 文件以进行测试,而不是直接打开 HTML 文件(即在文件资源管理器中双击它)。

所以使用 python3:

# in the directory that contains index.html:
python3 -m http.server 8080

访问 http://localhost:8080/index.html 对我有用。谢谢!

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号