在Javascript中使用相对路径来设置Google Map图标 [英] Using a Relative Path in Javascript to Set Google Map Icon

查看:101
本文介绍了在Javascript中使用相对路径来设置Google Map图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 图标:'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功能。



谢谢。

解决方案

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

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.

解决方案

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.

这篇关于在Javascript中使用相对路径来设置Google Map图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆