简体   繁体   English

谷歌地图更改标记图标

[英]google maps change Icon of marker

I am trying to change the marker icon and put my own icon with the the following code but insted of the icon nothing appears. 我试图更改标记图标,并使用以下代码放置我自己的图标,但插入该图标后,什么也没出现。 Any suggestions please? 有什么建议吗?

var marker = new google.maps.Marker({
    position: location,
    map: map,
    icon: 'c:\green.jpg'
});

You have to place your image (green.jpg) inside a folder that is accessible by your program. 您必须将图像(green.jpg)放在程序可访问的文件夹中。 Put it inside WAR folder for example. 例如,将其放在WAR文件夹中。 If you just have an HTML,PHP,... page, then put the image in the same folder 如果只有HTML,PHP,...页面,则将图像放在同一文件夹中

The icon value is expected to be a URI, so you probably need to pass it a file URI in this case. 图标值应为URI,因此在这种情况下,您可能需要向其传递文件URI。

file:///C:/green.jpg

Obviously if you're going to use this on the web then you will need to use a URL of something that is served by a web server, rather than an image file on your computer. 显然,如果要在Web上使用此功能,则需要使用Web服务器提供的内容的URL,而不是计算机上的图像文件。

Try using setIcon() function. 尝试使用setIcon()函数。 Something like marker.setIcon("iconfile") 类似于marker.setIcon(“ iconfile”)

More information about this function here: https://developers.google.com/maps/documentation/javascript/reference#Marker 有关此功能的更多信息,请访问: https : //developers.google.com/maps/documentation/javascript/reference#Marker

Search for: "setIcon(icon:string|MarkerImage)" 搜索:“ setIcon(icon:string | MarkerImage)”

You could try using the relative path to the file: 您可以尝试使用文件的相对路径:

var marker = new google.maps.Marker({
    position: location,
    map: map,
    icon: 'img/markers/brown_MarkerE.png'
});

I hope this helps. 我希望这有帮助。

You have to specify the protocol to use, in your case the file:// file transfer protocol 您必须指定要使用的协议,在您的情况下为file:// 文件传输协议

var marker = new google.maps.Marker({
    position: location,
    map: map,
    icon: 'http://c:/green.jpg'
});

Note: You have to store the image on your webserver to make it accessible to everyone. 注意:您必须将图像存储在Web服务器上,以使每个人都可以访问。 That way only user's with an image "grren.jpg" on there C partition will see the icon. 这样,只有具有C分区图像“ grren.jpg”的用户才能看到该图标。

here is the full document for that with example :
https://developers.google.com/maps/tutorials/customizing/custom-markers

you can also use this code:

var marker = new google.maps.Marker({
    position: location,
    map: map,
    icon: 'your_img.png'
});

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

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