在 Javascript 中使用相对路径设置 Google 地图图标

发布于 2024-12-08 17:49:02 字数 500 浏览 5 评论 0原文

我已使用绝对网址在 Google 地图中设置图标图像,如下所示:

 icon: 'http://mysite/wp-content/plugins/my_plugin/images/icon1.png'

我在此脚本中设置图像:http://mysite/wp-content/plugins/my_plugin/js/myscript.js

是否可以使用相对 URL? js脚本的路径是:../images/icon1.png 我已经尝试了我能想到的所有方法。 Google 地图位于 WordPress 插件中。

WordPress 有很多用于确定文件路径的 PHP 函数。我可以在 PHP 中保存文件路径,然后将其发送到 javascript 脚本,但我希望有更好、更简单的方法。我不想对图像文件路径进行硬编码,因为这将是另一件可能出错的事情。也许,有一些 JavaScript 函数可以在这种情况下工作。

谢谢。

I've set the icon image in my Google map using an absolute url as follows:

 icon: 'http://mysite/wp-content/plugins/my_plugin/images/icon1.png'

I set the image in this script: http://mysite/wp-content/plugins/my_plugin/js/myscript.js

Is it possible to use a relative URL? Would the path from the js script be: ../images/icon1.png I've tried every way that I can think of. The Google map is in a WordPress plugin.

WordPress has a lot of PHP functions for determining file paths. I could save file path in PHP and then send it to the javascript script, but I hope that there's a better, simpler way. I don't want to hardcode the image file path as it will be one more thing that can go wrong. Perhaps, there are javascript functions that would work in this case.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2024-12-15 17:49:02

相对路径应该可以正常工作,但它们需要相对于带有地图的页面的路径,而不是相对于JS脚本。如果您在需要不同相对路径的不同页面上使用地图脚本,那么以斜杠开头的路径(例如 /wp-content/plugins/my_plugin/images/icon1.png)是最好的选项。

Relative paths should work fine, but they need to be relative to the path of the page with the map, not relative to the JS script. If you're using the map script on different pages requiring different relative paths, than a path starting with a slash (e.g. /wp-content/plugins/my_plugin/images/icon1.png) is the best option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文