Using a Relative Path in Javascript to Set Google Map Icon

ぐ巨炮叔叔 提交于 2019-12-18 08:40:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7691251/using-a-relative-path-in-javascript-to-set-google-map-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!