在 Javascript 中使用相对路径设置 Google 地图图标
我已使用绝对网址在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相对路径应该可以正常工作,但它们需要相对于带有地图的页面的路径,而不是相对于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.