简体   繁体   English

在谷歌地图中添加本地图像作为自定义标记

[英]Add local image as custom marker in google maps

i'm trying to create a custom marker using google maps.我正在尝试使用谷歌地图创建自定义标记。

var iconBase = //some url;
var marker = new google.maps.Marker({
  position: myLatLng,
  map: map,
  icon: iconBase + 'marker.png'
});

This basically works.这基本上有效。 I host my images in a google drive.我在谷歌驱动器中托管我的图像。 It's good, but a little slow, takes about 500-700ms to load the image for the first time upon clicking to a map and rendering the marker.这很好,但有点慢,单击地图并渲染标记后第一次加载图像需要大约 500-700 毫秒。 So I'm trying to add the image locally, sadly, I can't seem to find resources regarding this one.所以我试图在本地添加图像,遗憾的是,我似乎无法找到有关此图像的资源。 How do I use local (in my project directory) images?如何使用本地(在我的项目目录中)图像?

Assuming I put it in a folder "icons" within the same directory level as my php file where I call google maps services, if that helps.假设我将它放在与我调用谷歌地图服务的 php 文件相同目录级别的文件夹“icons”中,如果有帮助的话。 Thank you!谢谢!

Update!更新!

I tried:我试过:

maker = new google.maps.Marker({
              position: location,
              map: globalMap,
              icon : "pubIcons/male-2.png"
            }); 

where pubIcons is a folder in the same directory as the php file where I render the map.其中pubIcons是与我渲染地图的 php 文件位于同一目录中的文件夹。 and I get this error :我收到此错误:

GET http://localhost/bims-2.0/index.php/location/view/pubIcons/male-2.png 404 (Not Found) 

It sort of behaves like pubIcons/male-2.png is an action in my controller "view", i'm using Yii btw.它有点像pubIcons/male-2.png是我的控制器“视图”中的一个动作,我正在使用 Yii 顺便说一句。

Try this code :)试试这个代码:)

var image = "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQxFoh469eOsZQkuPOLpZn3R6yyIExkZCxOxf4ywfeY3v330EwP3Q";
var marker = new google.maps.Marker({
        position: location,
        map: globalMap,
        icon: image
});

Update更新

var image  = "http://yourdomain.com/image/image.php";

or或者

icon : "/pubIcons/male-2.png"

I also came across this issue but resolved it a different way.我也遇到了这个问题,但以不同的方式解决了它。 I use Django for my web framework and Django makes use of a directory called static.我将 Django 用于我的 Web 框架,而 Django 使用一个名为 static 的目录。 Don't know if this dir can be used outside of Django.不知道这个目录是否可以在 Django 之外使用。 I loaded the pic into my static file and in my webpage's body tag, had the code我将图片加载到我的静态文件和我网页的正文标签中,有代码

<div style="display:none">

 `<img src="{% static 'map/CrocMarker_45.png' %}" alt="Croc Marker" id="crocMark">`

</div>

In this case, I set the display to none because I don't want to render the pic in the body tag, just want to load it so that I can access it in my maps scripts.在这种情况下,我将显示设置为无,因为我不想在 body 标签中渲染图片,只想加载它以便我可以在我的地图脚本中访问它。

In my function initMap() I create the variable var crocMark=document.getElementById("crocMark").src;在我的function initMap()我创建了变量var crocMark=document.getElementById("crocMark").src; . .

Later, when you're creating your markers, you can just set the icon to this variable.稍后,当您创建标记时,您只需将图标设置为此变量即可。 For example:例如:

var XMarker=new google.maps.Marker({ position: location, map: map_name, icon: crocMark });

This may not be the best way, but it worked great for me and I hope it does for you as well.这可能不是最好的方法,但它对我很有用,我希望它也适用于你。

Edit: Just a note, here's my project structure if it wasn't clear.编辑:请注意,如果不清楚,这是我的项目结构。

Project Root "Django">Django Root "Django_Project">My apps e.g. "map">static>map

The icon parameter of google.maps.Marker expects a URL. google.maps.Markericon参数需要一个 URL。 By specifying "pubIcons/male-2.png" , you were providing a relative URL, relative to the page you are on.通过指定"pubIcons/male-2.png" ,您提供了相对于您所在页面的相对 URL。

If you want to specify a file from the local file system, you need to use a file URL, for example: "file:///var/www/pubIcons/male-2.png" or wherever your file lives on the local file system.如果要从本地文件系统指定文件,则需要使用文件 URL,例如: "file:///var/www/pubIcons/male-2.png"或文件位于本地的任何位置文件系统。 Of course you'll only be able to access local files if you are loading the javascript from your local machine.当然,如果您从本地机器加载 javascript,您将只能访问本地文件。

Complete example:完整示例:

maker = new google.maps.Marker({
              position: location,
              map: globalMap,
              icon : "file:///var/www/pubIcons/male-2.png"
            }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM